public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");//此文件放在SRC目录下
Hello h=(Hello)context.getBean("Hello");
h.test1();//Hello类中文法
}
=========================================================================>
Hello类:
private JdbcTemplate jdbcTemplate ;
private AAA aaa;
private DataSourceTransactionManager transactionManager;
public DataSourceTransactionManager getTransactionManager() {
return transactionManager;
}
public void setTransactionManager(
DataSourceTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void test1(){
System.out.println(111);
}
=================================================================>
spring配置文件:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.0.203:1521:orcl</value>
</property>
<property name="username">
<value>YQZL</value>
</property>
<property name="password">
<value>YQZL</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="hello" name="hello" class="com.test.hello">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate" />
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
</bean>
</beans>