基本概念
java的spring框架中bean的注入集合,spring框架是java的SSH三大web开发框架之一。
一、依赖注入的概念:
(1)每个基于应用程序的java都有几个对象,这些对象一起工作来呈现出终端用户看到的工作的应用程序。
(2)Java应用程序时,应用程序类应该尽可能独立与其他Java类来增加这些类重用额可能性。依赖注入有时陪称为布线。
二、实现依赖注入的两种方式:(set方法注入和构造方法注入)
三、 Spring框架中bean的注入集合(list、set、map、props)
(1)Setter方法注和集合注入的结合代码
- 首先创建一个(.class的)POJO类来进行封装,在此之前在项目下面创建一个Source Folder包(包名lib,也可以自定义)然后导入bean、context、aop等包。
//实现封装List,set,Map的传值
public class PoJoDemo1 {
private List list;
private Set set;
private Map map;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
}
- 在src包下面新建一个.xml配置文件,并将类分别注入到容器中去。
<beans>
<bean id="pojo" class="com.anzhuo.cm.PoJoDemo1">
<!-- 实现把list,set,map集合注入到ListBing中 以下给list,set,map赋值-->
<property name="list">
<list>
<value>我是list1</value>
<value>我是list2</value>
</list>
</property>
<property name="set">
<set>
<value>我是set1</value>
<value>我是set2</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value="我是map1"></entry>
<entry key="1" value="我是map2"></entry>
</map>
</property>
</bean>
<bean id="list" class="com.anzhuo.cm.ListSMZQ">
<property name="pd" ref="pojo"></property>
</bean>
</beans>
- 创建一个(.class)类来实现依赖注入。
public class ListBing {
//实现依赖注入,这里是用set注入将你的值注入到我这里来
private PoJoDemo1 pd;
//实现set注入把PoJoDemo1里面封装的数据在spring里面配置给值后并且注入到我这里来了
public PoJoDemo1 getPd() {
return pd;
}
public void setPd(PoJoDemo1 pd) {
this.pd = pd;
}
public void ListBing(){
//在这里输出在POJODemo中疯转的数据值
System.out.println("我这里是PoJoDemo1里面封装的数据值:"
+pd.getList()+pd.getSet()+pd.getMap());
}
}
- 再创建一个.class的测试类。
public static void main(String[] args) {
//创建一个spring容器
ApplicationContext ac= new ClassPathXmlApplicationContext("Spring.xml");
//实例化ListBing对象并获取一个list的<bean>
ListBing list = (ListBing) ac.getBean("list");
list.ListBing();
}
(1)构造方法注入
- 首先创建一个(.class的)POJO类来进行封装,在此之前在项目下面创建一个Source Folder包(包名lib,也可以自定义)然后导入bean、context、aop等包。
public class POJObing{
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
- 在src包下面新建一个spring.xml(.xml)配置文件,并将类分别注入到容器中去。
<beans>
<bean id="PoJoDemo" class="com.anzhuo.cm.PoJobing">
<property name="signage" value="fish"></property>
<property name="address" value="湖南"></property>
<property name="coffin" value="鱼"></property>
</bean>
<bean id="lists" class="com.anzhuo.cm.ListLiu">
<constructor-arg name="pd" ref="PoJobing"></constructor-arg>
</bean>
</beans>
- 创建一个(.class)类来实现依赖注入。
public class ListLIu{
//创建一个构造方法
public ListLiu(PoJobing pd){
this.pd=pd;
}
public void Liu(){
//获取在POJObing类中的值,并输出
System.out.println("构造器注入,把它的值注入到我身体里面来了值为:"
+"----招牌:"+pd.getSignage()
+"----地址:"+pd.getAddress()
+"----主食材:"+pd.getCoffin());
}
}
- 再创建一个.class的测试类。
//创建一个spring容器
AbstractApplicationContext ac= new ClassPathXmlApplicationContext("Spring.xml");
ListLiu list = (ListLiu) ac.getBean("lists");
list.Liu();//调用ListLiu类中的Liu方法
四、注解注入
注解属性 | 描述 |
---|---|
@Required | @Required注解应用于bean属性的setter方法 |
@Autowired | @Autowired注解可以应用于bean属性的setter方法,非setter方法,构造函数和属性 |
@Qualifier | 通过指定确切的将被连线的bean,@Autowire和@Qualifier注解可以用来删除混乱。 |
JSR-250 Annotations | Spring支持JSR-250的基础注解,其中包括了@Resaurce、@PostConsuctor和@PreDestory注解 |
- @Required注解:
应用与bean属性的setter方法就是set方法的上面,它表明受影响的bean属性。在配置时必须方在xml配置文件中,否则容器就会抛出一个BeanInitializationException异常(Bean初始化异常)。 - @Autowired注解:
@Autowired注解可以在setter方法中被用于自动连接bean,就像@Autowired注解、容器、一个属性或者任意命名的可能带有多个参数的方法,当spring遇到一个在setter方法中使用的@Autowired注解,它会在方法中试图执行byType自动连接。 - @Qualifer注解:
使用@Qualifer注解和@Autowired注解通过指定哪一个真正的bean将会被装配来消除混乱。 - @ Resource注解:
在字段中或者sett方法中使用@Resource注解,@Resource注解使用一个‘name’属性,该属性以一个bean名称的形式被注入。用法:@ Resource(name=“对象”)。 - @configuration和@Bean注解
带有@configuraion的注解类表示这个类可以使用spring IOC容器作为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解将返回一个对象,该对象应该被注册在spring应用程序上下中的bean。
当@Bean依赖对方时,只要有一个bean方法调用另一个。