相关文章:http://hi.baidu.com/iduany/item/37ba4b3a833ffcc1382ffa70,关键是简单明了
这篇文章挺管用的
下面简单的列出实现注解的代码:
springtest.java
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
productCreator pe=(productCreator)ctx.getBean("productCreator");
pe.execPdao();
}
productCreator.java
public class productCreator {
@Resource
public personDao pDao=null;//注入personDao类
public productCreator(){
System.out.println("........正在被创建!");
}
public static personDao createPersonDao(){
return new personDao();
}
/****很奇怪,引入下面这段代码会报错,真不知道是什么原因*****/
// public personDao productAcreate(){
// return new personDao();
// }
public void execPdao(){
pDao.Test();
}
personDao,java
public void Test(){
System.out.println("hello,spring");
}
bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<!--
直接配置javabean
<bean id="persondao" class="springdao.personDao" scope="prototype">
</bean>
-->
<!-- 通过静态factory配置javabean
<bean id="productCreator" class="serviceImpl.productCreator" factory-method="createPersonDao">
</bean>
-->
<bean id="productCreator" class="serviceImpl.productCreator" scope="prototype"/>
<bean id="productAcreate" factory-bean="productCreator" factory-method="productAcreate" scope="singleton" lazy-init="true"/>
<bean id="personDao" class="springdao.personDao"/>
</beans>
运行结果:
........正在被创建!
hello,spring