spring注入Bean详解

首先建立工程导入Jar包,话不多说 上代码
dao层UserDao

package com.neusoft.dao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
//使用注解的方式配置 就不需要再xml文件中配置了
//Repository与上边效果相同
//@Component("userDao")
@Repository("userDao")
public class UserDao {
	public void init(){
		System.out.println("初始化userdao方法");
	}
	
	public void destroy(){
		System.out.println("销毁userdao方法");
	}
	public UserDao(){
		System.out.println("UserDao被实例化");
	}
	public void queryuser() {
		// TODO Auto-generated method stub
		System.out.println("UserDao的queryuser方法");
	}	
}

entity层ComplexType.java
这是复杂数据类型的bean类

package com.neusoft.entity;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

//复杂数据类型注入
public class ComplexType {
	private Object[] arr;
	private List list;
	private Set set;
	private Map map;
	private Properties property;
	public Object[] getArr() {
		return arr;
	}
	public void setArr(Object[] arr) {
		this.arr = arr;
	}
	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;
	}
	public Properties getProperty() {
		return property;
	}
	public void setProperty(Properties property) {
		this.property = property;
	}
	@Override
	public String toString() {
		return "ComplexType [arr=" + Arrays.toString(arr) + ", list=" + list + ", set=" + set + ", map=" + map
				+ ", property=" + property + "]";
	}
}

entity层Order.java
此bean类中有一个属性是另外一个bean也算小型复杂

package com.neusoft.entity;

public class Order {
	private int id ;
	private String note ;
	public User user;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@Override
	public String toString() {
		return "Order [id=" + id + ", note=" + note + ", user=" + user + "]";
	}
}

entity层User.java
此bean类为最简单的bean类

package com.neusoft.entity;
import java.io.Serializable;
public class User implements Serializable{
	public User(){};
	public User(int id,String name){
		this.id=id;
		this.name=name;
		System.out.println("User构造方法1");
	};
	public User(int id,String name,int age){
		this.id=id;
		this.name=name;
		this.age=age;
		System.out.println("User构造方法2");
	};
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id;
	private String name;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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 static long getSerialversionuid() {
		return serialVersionUID;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

factory层BusinessServiceFactory.java

package com.neusoft.factorytest;
import com.neusoft.service.BusinessService;
public class BusinessServiceFactory {
	public static BusinessService getInstance(){
		return new BusinessService();		
	}
}

factory层StaticUserFactory.java
静态实例工厂

package com.neusoft.factorytest;
import com.neusoft.entity.User;
//静态实例化工厂
public class StaticUserFactory {
	public static User getInstance(){
		return new User();		
	}
}

factory层UserFactory.java
非静态实例工厂

package com.neusoft.factorytest;
import com.neusoft.entity.User;
//非静态实例化工厂
public class UserFactory {
	public User getInstance(){
		return new User();		
	}
}

service层BusinessService.java
这是service层,便于后边测试

package com.neusoft.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.neusoft.dao.UserDao;
public class BusinessService {
	UserDao userdao=new UserDao();
	//Autowired根据类型 找到相同的 自动装载
	//Qualifier是相同的id时候如何自动装载
//	@Autowired
//	@Qualifier("userdao2")
	//Resource(name="userdao2")是上边两个的合集
	@Resource(name="userdao2")
	public void setUserdao(UserDao userdao) {
		this.userdao = userdao;
	}
	public void querybusiness(){
		userdao.queryuser();
	}
}

test层TestSpring1.java
这是测试层,这是使用junit4测试的

package com.neusoft.test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.neusoft.entity.ComplexType;
import com.neusoft.entity.Order;
import com.neusoft.entity.User;
import com.neusoft.factorytest.BusinessServiceFactory;
import com.neusoft.service.BusinessService;
import com.sun.org.apache.bcel.internal.util.ClassPath;

public class TestSpring1 {
	//原始方法(原始社会)需要什么NEW啥
	@Test
	public void test1(){
		BusinessService businessService=new BusinessService();
		businessService.querybusiness();
	}
	//半封建社会(使用工厂)
	@Test
	public void test2(){
		
		BusinessService businessService=BusinessServiceFactory.getInstance();
		businessService.querybusiness();
		}
	
	//现在社会(使用spring帮我们控制bean)
	//BeanFactory 懒加载
	@Test
	public void test3(){
		BeanFactory beanFactory=
new XmlBeanFactory(new ClassPathResource("config/applicationContext.xml"));
		//当使用get具体的实体类才会进行初始化  当前userdao被实例化
		beanFactory.getBean("userdao");
		}
	
//他俩的区别也是常问的面试问题
//BeanFactory 懒加载,需要时候才初始化bean,调用时需要初始化时间长,出现错误待调用时提示
//applicationContext 积极加载 获得容器时候就初始化所有bean,开启时间长,调用的时间短,获得容器时出现错误提示
	
//applicationContext 积极加载策略
		@Test
		public void test4(){
			//当我们获得spring 容器时 容器里变得bean就自动的被实例化
			//使用空间换取时间
			ApplicationContext applicationContext=
	new ClassPathXmlApplicationContext("config/applicationContext.xml");
			
			//如果xml中配置了lazy-init 则必须用以下语句实例化
			applicationContext.getBean("userdao");
			}
		
		//静态实例工厂
		@Test
		public void test5(){
			ApplicationContext applicationContext=
	new ClassPathXmlApplicationContext("config/applicationContext.xml");
		User user=	(User) applicationContext.getBean("staticuserfactory");
			System.out.println(user);
		}
		//非静态实例工厂(实例工厂)
		@Test
		public void test6(){
			ApplicationContext applicationContext=
	new ClassPathXmlApplicationContext("config/applicationContext.xml");
		User user=	(User) applicationContext.getBean("staticuserfactory");
			System.out.println(user);
		}
		
		
		//构造器注入值constructor
		@Test
		public void test7(){
			ApplicationContext applicationContext=
	new ClassPathXmlApplicationContext("config/applicationContext.xml");
		User user=	(User) applicationContext.getBean("constructoruserdao");
			System.out.println(user);
		}
		
		
		//构造器注入值property
		@Test
		public void test8(){
			ApplicationContext applicationContext=
	new ClassPathXmlApplicationContext("config/applicationContext.xml");
		User user=	(User) applicationContext.getBean("propertyuser");
			System.out.println(user);
		}
		
		//构造器注入值property+ref bean
		@Test
		public void test9(){
			ApplicationContext applicationContext=
	new ClassPathXmlApplicationContext("config/applicationContext.xml");
		Order order=(Order) applicationContext.getBean("propertyorder");
			System.out.println(order);
		}
		
		//复杂数据类型注入Bean
		@Test
		public void test10(){
			ApplicationContext applicationContext=
	new ClassPathXmlApplicationContext("config/applicationContext.xml");
			ComplexType complexType=(ComplexType) applicationContext.getBean("complextype");
			System.out.println(complexType);
		}
		
		//Bean传入service层
		@Test
		public void test11(){
			ApplicationContext applicationContext=
	new ClassPathXmlApplicationContext("config/applicationContext.xml");
			BusinessService businessService=(BusinessService) applicationContext.getBean("businessService");
			businessService.querybusiness();
		}
}

在此类中详细解释了各个测试对应的类型

最重要的XML配置问题,其中有各种注入的详细解释。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
 <context:component-scan base-package="com.neusoft" ></context:component-scan>

<bean id="businessService" class="com.neusoft.factorytest.BusinessServiceFactory">
<!-- <property name="userdao">
<ref bean="propertyuser"/>
</property> -->
</bean>

<bean id="userdao" class="com.neusoft.dao.UserDao"  
lazy-init="true" init-method="init" destroy-method="destroy"
scope="propotype"></bean>
<bean id="userdao2" class="com.neusoft.dao.UserDao"  scope="singleton"></bean>

<!-- 通过构造器实例化  -->
<bean id="user" class="com.neusoft.entity.User"  ></bean>
<!-- 通过静态工厂 -->
<bean id="staticuserfactory" class="com.neusoft.factorytest.StaticUserFactory"  
factory-method="getInstance"></bean>
<!-- 通过实例工厂  -->
<bean id="user1" class="com.neusoft.entity.User"  factory-bean="createuserfactory"></bean>
<bean id="createuserfactory" class="com.neusoft.factorytest.UserFactory" ></bean>
<!-- 构造器注入 constructor-arg-->
<bean id="constructoruserdao" class="com.neusoft.entity.User">
<constructor-arg type="int" value="11" index="0" name="id"></constructor-arg>
<constructor-arg type="String" value="Tom" index="1" name="name"></constructor-arg>
</bean>
<!-- 构造器注入property -->
<bean id="propertyuser" class="com.neusoft.entity.User">
<property name="id">
<value>10</value>
</property>
<property name="name">
<value>Jerry</value>
</property>
</bean>

<!-- 构造器注入property+ref -->
<bean id="propertyorder" class="com.neusoft.entity.Order">
<property name="id">
<value>1</value>
</property>
<property name="note">
<value>hahaha</value>
</property>
<property name="user">
<ref bean="propertyuser"/>
</property>
</bean>

<!-- 构造器注入复杂数据类型 array list set map property-->
<bean id="complextype" class="com.neusoft.entity.ComplexType">
<property name="arr">
	<array>
		<value>aa</value>
		<value>bb</value>
		<ref bean="propertyuser"/>
	</array>
</property>
<property name="list">
	<list>
	<value>cc</value>
	<value>dd</value>
	<ref bean="propertyuser"/>
	</list>
</property>
<property name="set">
	<set>
	<value>ee</value>
	<value>ff</value>
	<ref bean="propertyuser"/>
	</set>
</property>
<property name="map">
	<map>
		<entry key="aa" >
		<value>aa123</value>
		</entry>
		<entry key="bb" >
		<value>bb123</value>
		</entry>
		<entry key="cc" >
		<ref bean="propertyuser"/>
		</entry>
	</map>
</property>
<property name="property">
	<props>
	<prop key="a1">a1111</prop>
	<prop key="a2">a2222</prop>
	<prop key="a3">a3333</prop>
	</props>
</property>
</bean>
</beans>

以上便是所有注入类型的解答,下次更新AOP切面编程相关问题。

猜你喜欢

转载自blog.csdn.net/ZhouhbUp/article/details/82856260