Spring之依赖注入配置和注解

一、基本属性注入

//实体的bean类
public class User {

	private Long id;
	private String name = "无名氏";
	private boolean gender; // 性别

	private Set<String> addressSet; // Set集合
	private Set numberSet; // Set集合(Integer型)

	private List<String> addressList; // List
	private String[] addressArray;
	private Map<String, String> addressMap; // Map
	private Properties properties;

	public User() {
	}

	public User(Long id, String name) {
		this.id = id;
		this.name = name;
	}
}
	<!-- 通过属性注入(setter方法) -->
	<bean name="user" class="pde.ams.user.h_beanproperty.User">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="gender" value="false"></property>
	</bean>

	<!-- 通过构造方法注入 -->
	<bean name="user2" class="pde.ams.user.h_beanproperty.User">
		<constructor-arg value="2"></constructor-arg>
		<constructor-arg value="李四"></constructor-arg>
	</bean>

	<!-- 测试属性的注入,Spring可以自动转换类型 -->
	<bean name="user3" class="pde.ams.user.h_beanproperty.User">
		<property name="id" value="1"></property>
		<property name="name">
			<null />
		</property>
		<property name="gender" value="true"></property>

		<!-- Set集合(String型) -->
		<property name="addressSet">
			<set>
				<value>三里屯东路</value>
				<value>三里屯科贸园</value>
			</set>
		</property>

		<!-- Set集合(Integer型) 如果指定强类型,如Set<Integer>,则配置时可以不指定type,Spring能自动转换。 如果没有指定类型,默认当成String处理。这时指定明确的指定了类型,Spring才可以进行转换 -->
		<property name="numberSet">
			<set>
				<value type="java.lang.Integer">10</value>
				<value type="java.lang.Integer">20</value>
				<value type="java.lang.Integer">30</value>
			</set>
		</property>

		<!-- List集合 -->
		<property name="addressList">
			<list>
				<value>三里屯3</value>
				<value>三里屯2</value>
				<value>三里屯1</value>
			</list>
		</property>

		<!-- 数组,与List集合的配置一样 -->
		<property name="addressArray">
			<list>
				<value>三里屯东路1</value>
				<value>三里屯东路2</value>
				<value>三里屯东路3</value>
			</list>
		</property>

		<!-- Map集合 -->
		<property name="addressMap">
			<map>
				<entry key="地址1" value="三里屯东路1"></entry>
				<entry key="地址2" value="三里屯东路2"></entry>
			</map>
		</property>

		<!-- Properties集合 -->
		<property name="properties">
			<props>
				<prop key="pageSize">20</prop>
				<prop key="maxFileSize">1000K</prop>
			</props>
		</property>
	</bean>

	<!-- 配置dao,如果这个bean只是用于抽取出的多个bean的公共配置,则加上abstract="true", 这时就不需要写class属性。 -->
	<bean id="baseDao" abstract="true">
		<property name="dataSource" value="myDataSource"></property>
	</bean>

	<!-- 在其他bean中,可以指定parent属性,表示要从哪个父bean继承过来一些公共的配置 -->
	<bean id="myUserDao" parent="baseDao"
		class="pde.ams.user.h_beanproperty.UserDao">
	</bean>

	<bean id="myRoleDao" parent="baseDao"
		class="pde.ams.user.h_beanproperty.RoleDao">
	</bean>
二、依赖的注入
public class UserDao {
	
	private String dataSource; // 数据源,应是DataSource类型,这里只是模拟一下

	public void saveUser(Object user) {
		System.out.println("UserDao.saveUser()");
	}

	public String getDataSource() {
		return dataSource;
	}

	public void setDataSource(String dataSource) {
		this.dataSource = dataSource;
	}
	// ...
}
public class UserService {

	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	// ...
}
<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="userDao" class="pde.ams.user.i_property_ref.UserDao">
		<property name="dataSource" value="dataSource1"></property>
	</bean>

	<!-- 配置UserService,需要依赖UserDao,使用ref属性指定 -->
	<bean id="userService" class="pde.ams.user.i_property_ref.UserService">
		<property name="userDao" ref="userDao"></property>
	</bean>


	<!-- 可以在property元素下使用子元素bean声明一个匿名的bean,只是给当前的属性设置使用。 这时不需要指定id或是name属性,指定了也是无效的。 -->
	<bean id="userService2" class="cn.itcast.spring.i_beanproperty_ref.UserService">
		<property name="userDao">
			<bean class="cn.itcast.spring.i_beanproperty_ref.UserDao">
				<property name="dataSource" value="dataSource2"></property>
			</bean>
		</property>
	</bean>s


</beans>

三、注解方式注入

@Repository("userDao")
@Scope("prototype")
public class UserDao {
	
	private String dataSource; // 数据源,应是DataSource类型,这里只是模拟一下

	public void saveUser(Object user) {
		System.out.println("UserDao.saveUser()");
	}
} 
@Service
public class UserService {

	@Resource
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	// ...
}
 
 
<?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:tx="http://www.springframework.org/schema/tx"
	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
		http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">


	<!-- 设置为自动扫描与装配置bean,base-package是指定要扫描哪个包,可以使用逗号隔开的多个包名 -->
	<context:component-scan base-package="cn.itcast.spring.j_beanproperty_annonation"></context:component-scan>

猜你喜欢

转载自blog.csdn.net/weixin_40931184/article/details/80300163