main方法中呼叫spring注入bean
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
<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>