1 package com.spring.model;
2
3 public class Tiger {
4
5 private String tigerName="TigerKing";
6
7 public String toString(){
8 return "TigerName:"+tigerName;
9 }
10 }
1 package com.spring.model;
2
3 public class Monkey {
4
5 private String monkeyName = "MonkeyKing";
6
7 public String toString(){
8 return "MonkeyName:" + monkeyName;
9 }
10
11 }
1 package com.spring.model;
2
3 public class Zoo {
4 private Tiger tiger;
5 private Monkey monkey;
6
7 public Tiger getTiger() {
8 return tiger;
9 }
10 public void setTiger(Tiger tiger) {
11 this.tiger = tiger;
12 }
13 public Monkey getMonkey() {
14 return monkey;
15 }
16 public void setMonkey(Monkey monkey) {
17 this.monkey = monkey;
18 }
19
20 public String toString(){
21 return tiger + "\n" + monkey;
22 }
23
24 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 "> 12 13 <bean id="zoo" class="com.spring.model.Zoo" > 14 <property name="tiger" ref="tiger" /> 15 <property name="monkey" ref="monkey" /> 16 </bean> 17 18 <bean id="tiger" class="com.spring.model.Tiger" /> 19 <bean id="monkey" class="com.spring.model.Monkey" /> 20 21 </beans>
1 public class TestAnnotation {
2 /**
3 * 不使用注解
4 */
5 @Test
6 public void test(){
7 //读取配置文件
8 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext2.xml");
9 Zoo zoo=(Zoo) ctx.getBean("zoo");
10 System.out.println(zoo.toString());
11 }
12 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 "> 12 13 <context:component-scan base-package="com.spring" /> 14 15 <bean id="zoo" class="com.spring.model.Zoo" /> 16 <bean id="tiger" class="com.spring.model.Tiger" /> 17 <bean id="monkey" class="com.spring.model.Monkey" /> 18 19 </beans>
1 package com.spring.model;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4
5 public class Zoo {
6
7 @Autowired
8 private Tiger tiger;
9
10 @Autowired
11 private Monkey monkey;
12
13 public String toString(){
14 return tiger + "\n" + monkey;
15 }
16
17 }
1 package com.spring.model;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4
5 public class Zoo {
6
7 @Autowired(required=false)
8 private Tiger tiger;
9
10 @Autowired(required=false)
11 private Monkey monkey;
12
13 public String toString(){
14 return tiger + "\n" + monkey;
15 }
16
17 }
1 package com.spring.service;
2
3 public interface ICar {
4
5 public String getCarName();
6 }
1 package com.spring.service.impl;
2
3 import com.spring.service.ICar;
4
5 public class BMWCar implements ICar{
6
7 public String getCarName(){
8 return "BMW car";
9 }
10 }
1 package com.spring.service.impl;
2
3 import com.spring.service.ICar;
4
5 public class BenzCar implements ICar{
6
7 public String getCarName(){
8 return "Benz car";
9 }
10 }
1 package com.spring.model;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4
5 import com.spring.service.ICar;
6
7 public class CarFactory {
8
9 @Autowired
10 private ICar car;
11
12 public String toString(){
13 return car.getCarName();
14 }
15
16 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 "> 12 13 <context:component-scan base-package="com.spring" /> 14 15 <!-- Autowired注解配合Qualifier注解 --> 16 <bean id="carFactory" class="com.spring.model.CarFactory" /> 17 <bean id="bmwCar" class="com.spring.service.impl.BMWCar" /> 18 <bean id="benz" class="com.spring.service.impl.BenzCar" /> 19 20 </beans>
1 /**
2 * Autowired注解配合Qualifier注解
3 */
4 @Test
5 public void test1(){
6 //读取配置文件
7 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext2.xml");
8 CarFactory carFactory=(CarFactory) ctx.getBean("carFactory");
9 System.out.println(carFactory.toString());
10 }
1 package com.spring.model;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.beans.factory.annotation.Qualifier;
5
6 import com.spring.service.ICar;
7
8 public class CarFactory {
9
10 @Autowired
11 @Qualifier("bmwCar")
12 private ICar car;
13
14 public String toString(){
15 return car.getCarName();
16 }
17
18 }
1 package com.spring.model;
2
3 import javax.annotation.Resource;
4
5 public class Zoo1 {
6
7 @Resource(name="tiger")
8 private Tiger tiger;
9
10 @Resource(type=Monkey.class)
11 private Monkey monkey;
12
13 public String toString(){
14 return tiger + "\n" + monkey;
15 }
16 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 "> 12 13 <context:component-scan base-package="com.spring" /> 14 15 </beans>
1 package com.spring.model;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.stereotype.Service;
5
6 @Service
7 public class Zoo {
8
9 @Autowired
10 private Tiger tiger;
11
12 @Autowired
13 private Monkey monkey;
14
15 public String toString(){
16 return tiger + "\n" + monkey;
17 }
18
19 }
1 package com.spring.model;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.context.annotation.Scope;
5 import org.springframework.stereotype.Service;
6
7 @Service("Zoo")
8 @Scope("prototype")
9 public class Zoo {
10
11 @Autowired
12 private Tiger tiger;
13
14 @Autowired
15 private Monkey monkey;
16
17 public String toString(){
18 return tiger + "\n" + monkey;
19 }
20
21 }
1 <context:component-scan base-package="cn.gacl.java"/>
1 <context:component-scan base-package="cn.gacl.dao.impl,cn.gacl.service.impl,cn.gacl.action"/>
1 @Controller
2 @Scope("prototype")
3 public class UserAction extends BaseAction<User>{
4 ……
5 }
使用@Controller注解标识UserAction之后,就表示要把UserAction交给Spring容器管理,在Spring容器中会存在一个名字为"userAction"的action,这个名字是根据UserAction类名来取的。注意:如果@Controller不指定其value【@Controller】,则默认的bean名字为这个类的类名首字母小写,如果指定value【@Controller(value="UserAction")】或者【@Controller("UserAction")】,则使用value作为bean的名字。
这里的UserAction还使用了@Scope注解,@Scope("prototype")表示将Action的范围声明为原型,可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,避免struts中Action的线程安全问题。spring 默认scope 是单例模式(scope="singleton"),这样只会创建一个Action对象,每次访问都是同一Action对象,数据不安全,struts2 是要求每次次访问都对应不同的Action,scope="prototype" 可以保证当有请求的时候都创建一个Action对象
1 @Service("userService")
2 public class UserServiceImpl implements UserService {
3 ………
4 }
@Service("userService")注解是告诉Spring,当Spring要创建UserServiceImpl的的实例时,bean的名字必须叫做"userService",这样当Action需要使用UserServiceImpl的的实例时,就可以由Spring创建好的"userService",然后注入给Action:在Action只需要声明一个名字叫“userService”的变量来接收由Spring注入的"userService"即可,具体代码如下:
1 // 注入userService 2 @Resource(name = "userService") 3 private UserService userService;
注意:在Action声明的“userService”变量的类型必须是“UserServiceImpl”或者是其父类“UserService”,否则由于类型不一致而无法注入,由于Action中的声明的“userService”变量使用了@Resource注解去标注,并且指明了其name = "userService",这就等于告诉Spring,说我Action要实例化一个“userService”,你Spring快点帮我实例化好,然后给我,当Spring看到userService变量上的@Resource的注解时,根据其指明的name属性可以知道,Action中需要用到一个UserServiceImpl的实例,此时Spring就会把自己创建好的名字叫做"userService"的UserServiceImpl的实例注入给Action中的“userService”变量,帮助Action完成userService的实例化,这样在Action中就不用通过“UserService userService = new UserServiceImpl();”这种最原始的方式去实例化userService了。如果没有Spring,那么当Action需要使用UserServiceImpl时,必须通过“UserService userService = new UserServiceImpl();”主动去创建实例对象,但使用了Spring之后,Action要使用UserServiceImpl时,就不用主动去创建UserServiceImpl的实例了,创建UserServiceImpl实例已经交给Spring来做了,Spring把创建好的UserServiceImpl实例给Action,Action拿到就可以直接用了。Action由原来的主动创建UserServiceImpl实例后就可以马上使用,变成了被动等待由Spring创建好UserServiceImpl实例之后再注入给Action,Action才能够使用。这说明Action对“UserServiceImpl”类的“控制权”已经被“反转”了,原来主动权在自己手上,自己要使用“UserServiceImpl”类的实例,自己主动去new一个出来马上就可以使用了,但现在自己不能主动去new“UserServiceImpl”类的实例,new“UserServiceImpl”类的实例的权力已经被Spring拿走了,只有Spring才能够new“UserServiceImpl”类的实例,而Action只能等Spring创建好“UserServiceImpl”类的实例后,再“恳求”Spring把创建好的“UserServiceImpl”类的实例给他,这样他才能够使用“UserServiceImpl”,这就是Spring核心思想“控制反转”,也叫“依赖注入”,“依赖注入”也很好理解,Action需要使用UserServiceImpl干活,那么就是对UserServiceImpl产生了依赖,Spring把Acion需要依赖的UserServiceImpl注入(也就是“给”)给Action,这就是所谓的“依赖注入”。对Action而言,Action依赖什么东西,就请求Spring注入给他,对Spring而言,Action需要什么,Spring就主动注入给他。
1 @Repository(value="userDao")
2 public class UserDaoImpl extends BaseDaoImpl<User> {
3 ………
4 }
@Repository(value="userDao")注解是告诉Spring,让Spring创建一个名字叫“userDao”的UserDaoImpl实例。
当Service需要使用Spring创建的名字叫“userDao”的UserDaoImpl实例时,就可以使用@Resource(name = "userDao")注解告诉Spring,Spring把创建好的userDao注入给Service即可。1 // 注入userDao,从数据库中根据用户Id取出指定用户时需要用到 2 @Resource(name = "userDao") 3 private BaseDao<User> userDao;
<context:component-scan base-package="cn.test"/>