今天胡老师询问是否可以在java中读取配置文件,并询问了java操纵数据库的情况。我都给出了肯定的回答。晚上上不了迅雷看看,随手从以前的代码中按照胡老师的需要抽取了一部分,装配成型。主要用到的Spring的ioc,jdbctmplate,以及rowmapper。上代码:
package sever.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class GetBean {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans-config.xml");
Settings settings = (Settings) context.getBean("settings");
System.out.println("the config1 of Settings is: "
+ settings.getConfig1());
SimpleJdbcUtil sju = new SimpleJdbcUtil();
User[] users = sju.fetchall();
for (User u : users) {
System.out.println("the name of User u is: " + u.getUserName());
}
}
}
package sever.bean;
public class SimpleJdbcUtil extends SpringJdbcUtil {
@SuppressWarnings("unchecked")
public User[] fetchall() {
String sql = "select * from user";
return (User[]) this.getJdbcTemplate().query(sql, new UserRowMapper())
.toArray(new User[0]);
}
}
package sever.bean;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class UserRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
String phoneNO = rs.getString("phoneno");
String phoneNO1 = rs.getString("phoneno1");
String phoneNO2 = rs.getString("phoneno2");
String companyname = rs.getString("companyname");
String address = rs.getString("address");
User user = new User(username, password, email, phoneNO, phoneNO1,
phoneNO2, companyname, address);
return user;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>Config.txt</value>
</list>
</property>
</bean>
<bean id="settings"
class="sever.bean.Settings"
destroy-method="close">
<property name="config1" value="${Config.config1}" />
</bean>
</beans>
# Properties file with JDBC-related settings.
# Applied by PropertyPlaceholderConfigurer from "applicationContext.xml".
# Targeted at system administrators, to avoid touching the context XML files.
Config.config1=I am config1
# Config.config2=I am config2
分享到:
相关推荐
getBean开始Bean 是否 不为null并且args为null调用getObjectForBeanInstance方法停止调用AbstractBeanFa
spring Bean的生命周期
JdbcTemplate jdTemplate =(JdbcTemplate) applicationContext.getBean("jdbcTemplate"); jdTemplate.execute("create table account(" id int primary key auto_increment," + "username varchar(50)," + "balance...
加载spring配置文件,提供getBean接口.
今天小编就为大家分享一篇关于spring中通过ApplicationContext getBean获取注入对象的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
这里就要导入mybatis和spring的整合包了,这里sqlsession中也要导入mybatis的配置文件 2.6 spring-bean 配置mapper自动扫描 MapperScannerConfigurer将扫描basePackage所指定的包下的所有接口类(包括子类), ...
处理ssh组合框架中如何用getBean获取实体 在ssh中不能使用练习时候用的方法 初始化 applicationCon..文件 所以使用该方法也可以获取 getBean方法!
Spring模拟,可创建对象和spring getbean一样,支持简单AOP,可用于dao,service,web层接偶。用注解实现。必须在service定义dao层一个接口,那么该工具将自动在配置的扫描bean中找到该类(接口)的实现类(不必再...
spring+hibernate+osworkflow,服务器为Tomcat6.0,数据库为Mysql 把viewlivegraph2.jsp改为viewlivegraph.jsp. 其中把wf的获得修改为 ApplicationContext cxt = WebApplicationContextUtils.get...
jfinal 控制器 接收数据
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/368874
spring技术入门系列源码 public class SpringTest { public static void main(String[] args) { //创建spring的ApplicationContext ApplicationContext ctx = new ClassPathXmlApplicationContext(...
解析了spring的底层实现机制。public interface BeanFactory { //这里是对FactoryBean的转义定义,因为如果使用bean的名字检索FactoryBean得到的对象是工厂生成的对象, //如果需要得到工厂本身,需要转义 String ...
通过Spring集成单元测试,以后测试service层内容时,就不用getBean()来获得Service层对象了
与singleton相反, 每次通过容器的getBean()方法获取该作用域下的Bean时都将产生一个新的Bean实例 3>request: 对于同一次Http请求,request作用域下的Bean都将只生成一个实例。这种作用域只有在Web应用中使用Spring...
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 在配置文件中注入<bean class="xxx.xxx.SpringContextHolder" lazy-init="false" /> 即可使用
在这里,我们分别定义了一个MessageSender对象(messageSenderImpl)和一个Before Advice对象(logBeforeAdvice),并定义了一个 org.springframework.aop.framework.ProxyFactoryBean对象(messageSender),...
* lazy-init为"true"当context.getBean时创建 bean为多例时,必须用这种方案创建对象 * 不能及时发现错误 * 数据会在需要的时候加载 * 初始化 * 由spring容器调用init方法 * 在构造函数之后执行 * 销毁 * ...
该项目中注入jedisPool时使用了getBean()注入,然而怎么跑都是NullPoint,一翻扣头发后得已解决。 解决方法1 使用@Autowired的方式注入 必须在类上加@Component。我是用的这种 @Component public class ...