Spring之注解注入bean
注:本文来源于: Elinice 《Spring之注解注入bean》
1.引入jar包。配置log4j.properties文件
2.创建Person类
1 package com.hy.spring.pojo; 2 3 import org.springframework.stereotype.Component; 4 5 //使用component的注解方式注入 6 //@Component("person"),等同于使用依赖注入方式,配置的<bean name="person" class="com.hy.spring.pojo.Person"></bean> 7 @Component("person") 8 public class Person { 9 10 @Override 11 public String toString() { 12 return "Person [name=" + name + ", age=" + age + ", mycar=" + mycar + "]"; 13 } 14 15 private String name; 16 private Integer age; 17 private Car mycar; 18 19 public Person() { 20 super(); 21 System.out.println("构造方法被调用"); 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 public Integer getAge() { 33 return age; 34 } 35 36 public void setAge(Integer age) { 37 this.age = age; 38 } 39 40 public Car getMycar() { 41 return mycar; 42 } 43 44 public void setMycar(Car mycar) { 45 this.mycar = mycar; 46 } 47 } 48 49
创建Car类
1 package com.hy.spring.pojo; 2 3 4 public class Car { 5 6 private String name; 7 private String color; 8 9 @Override 10 public String toString() { 11 return "Car [name=" + name + ", color=" + color + "]"; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 public String getColor() { 20 return color; 21 } 22 public void setColor(String color) { 23 this.color = color; 24 } 25 } 26 27
3.配置applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd"> 10 11 <context:component-scan base-package="com.hy.spring.pojo"></context:component-scan> 12 13 </beans> 14
4.编写测试用例
1 package com.hy.spring.pojo; 2 3 import javax.annotation.Resource; 4 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 10 //创建容器 11 @RunWith(SpringJUnit4ClassRunner.class) 12 @ContextConfiguration("classpath:applicationContext2.xml") 13 public class RunWithTest { 14 15 16 //在bean容器中查找name为person的对象,就是我们之前在Person类中用注解component创建的bean 17 @Resource(name="person") 18 private Person p; 19 20 @Test 21 public void test() { 22 System.out.println(p); 23 } 24 } 25 26
总结
1.其他类级别的注解
1 @Component("person") //适用于所有层 2 @Service("person") //适用于Service层 3 @Repository("person") //适用于持久层 4 @Controller("person") //适用于Controller层 5
2.限制单例或是多例
1 @Component("person") //适用于所有层 2 @Scope(scopeName="singleton")//限制创建单例对象 3 //@Scope(scopeName="prototype")//限制创建多例对象 4
3.set方式注入value值
- 在私有的成员变量注入
1 @Value("BMW") 2 private String name; 3 @Value("Red") 4 private String color; 5
- 在set方法中注入
1 @Value("BMW") 2 public void setName(String name) { 3 this.name = name; 4 } 5 @Value("Red") 6 public void setColor(String color) { 7 this.color = color; 8 } 9
4.自动装配注解,@Autowired
注意,此处按照对象的类型进行自动装配
1 @Autowired 2 private Car mycar; 3
自动装配的例子
1.Person类
1 package com.hy.spring.pojo; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.stereotype.Component; 6 7 @Component("person") 8 public class Person { 9 10 @Override 11 public String toString() { 12 return "Person [name=" + name + ", age=" + age + ", mycar=" + mycar + "]"; 13 } 14 15 private String name; 16 private Integer age; 17 18 @Autowired 19 private Car mycar; 20 21 public Person() { 22 super(); 23 System.out.println("构造方法被调用"); 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 @Value("Tom") 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 36 public Integer getAge() { 37 return age; 38 } 39 40 @Value("18") 41 public void setAge(Integer age) { 42 this.age = age; 43 } 44 45 public Car getMycar() { 46 return mycar; 47 } 48 49 50 public void setMycar(Car mycar) { 51 this.mycar = mycar; 52 } 53 54 } 55
2.Car类
1 package com.hy.spring.pojo; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.stereotype.Component; 5 6 @Component("car") 7 public class Car { 8 9 @Value("BMW") 10 private String name; 11 @Value("Red") 12 private String color; 13 14 @Override 15 public String toString() { 16 return "Car [name=" + name + ", color=" + color + "]"; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public String getColor() { 25 return color; 26 } 27 public void setColor(String color) { 28 this.color = color; 29 } 30 } 31
3.applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd"> 10 11 <context:component-scan base-package="com.hy.spring.pojo"></context:component-scan> 12 13 </beans> 14
4.测试类
1 package com.hy.spring.pojo; 2 3 4 import javax.annotation.Resource; 5 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 import org.springframework.test.context.ContextConfiguration; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 11 12 @RunWith(SpringJUnit4ClassRunner.class) 13 @ContextConfiguration("classpath:applicationContext2.xml") 14 public class RunWithTest { 15 16 @Resource(name="person") 17 private Person p; 18 19 @Test 20 public void test() { 21 System.out.println(p); 22 } 23 } 24
5.运行结果,控制台输出信息:
自动装配可能存在的问题,一个类型有多个对象,自动装配不知道装配哪个对象
如:假设applicationContext.xml配置为
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd"> 10 11 <bean name="car1" class="com.hy.spring.pojo.Car"> 12 <property name="name" value="BMW1"></property> 13 <property name="color" value="red"></property> 14 </bean> 15 16 <bean name="car2" class="com.hy.spring.pojo.Car"> 17 <property name="name" value="BMW2"></property> 18 <property name="color" value="blue"></property> 19 </bean> 20 21 </beans> 22
1.可以用@Qualifier(“name”)解决,如下:
1 @Autowired 2 @Qualifier("car1") 3 private Car mycar; 4
2.可以用@Resource(name=“name”)解决,如下:
1 @Resource(name="car1") 2 private Car mycar; 3
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/