67,513
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" >
<mvc:annotation-driven />
<context:annotation-config/>
<context:component-scan base-package="com.memo.controller" />
<context:component-scan base-package="com.memo.dao" />
<mvc:resources mapping="/img/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/javascript/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/memo</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="dataBean" class="com.memo.dao.ConnectionFactory">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!-- 视图解析器,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/html/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
private static final Logger log = Logger.getLogger(ConnectionFactory.class);
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public DataSource getDataSource(){
return this.dataSource;
}
public void query()throws Exception{
log.info("dataSource:"+dataSource);
Connection conn = dataSource.getConnection();
System.out.println("conn:"+conn);
}
public static void main(String[] args) throws Exception{
new ConnectionFactory().query();
}
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/memo");
dataSource.setUsername("root");
dataSource.setPassword("123456");
Connection conn = dataSource.getConnection();
@Autowired
DataSource dataSource;
作用呢?楼主小白,多谢指导。
在web.xml的中配置:(注:上面的测试类,应该是通过web请求之后才会请求数据库链接)
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/config/applicationContext-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
beanFactory..getBean("dataBean", ConnectionFactory.class);
来获取ConnectionFactory的对象,这样它的dataSource属性就自动注入到了。
<bean id="dataBean" class="com.memo.dao.ConnectionFactory">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
测试类中是这样子:
@Autowired
DataSource dataSource;
public void query()throws Exception{
log.info("dataSource:"+dataSource);
Connection conn = dataSource.getConnection();
System.out.println("conn:"+conn);
}
public static void main(String[] args) throws Exception{
new ConnectionFactory().query();
}
使用自动注入的方式,单获得的datasource为null。BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"applicationContext.xml"));
DataSource dataSource = beanFactory.getBean("dataSource", DataSource.class);
System.out.println(dataSource);