首页 > 代码库 > Spring——IOC(二)
Spring——IOC(二)
三、Id和Name
在Spring的配置文件中,bean的id属性和name属性可以相互替换。
(1)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- bean也可以使用name来标识 -->
<bean name="u" class="com.zgy.impl.UserDAOImpl">
</bean>
<bean id="userService" class="com.zgy.service.UserService" scope="prototype">
<!-- <property name="userDAO" ref="u" /> -->
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>
</beans>
(2)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- bean也可以使用name来标识 -->
<bean name="u" class="com.zgy.impl.UserDAOImpl">
</bean>
<bean name="userService" class="com.zgy.service.UserService" scope="prototype">
<!-- <property name="userDAO" ref="u" /> -->
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>
</beans>
四、简单属性注入
可以在Spring的配置文件中配置简单属性的值
范例:
(1)UserDAOImpl中有两个属性
package com.bjsxt.dao.impl;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserDAOImpl implements UserDAO {
/* (non-Javadoc)
* @see com.bjsxt.dao.UserDAO#save(com.bjsxt.model.User)
*/
private int daoId;
private String daoStatus;
public int getDaoId() {
return daoId;
}
public void setDaoId(int daoId) {
this.daoId = daoId;
}
public String getDaoStatus() {
return daoStatus;
}
public void setDaoStatus(String daoStatus) {
this.daoStatus = daoStatus;
}
public void save(User user) {
System.out.println("user saved!");
}
@Override
public String toString() {
return this.daoId + ":" + this.daoStatus;
}
}
(2)在配置文件中配置这两个属性的值
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- bean也可以使用name来标识 -->
<bean name="userDAO" class="com.zgy.impl.UserDAOImpl">
<property name="daoId" value="8"></property>
<property name="daoStatus" value="good"></property>
</bean>
<bean id="userService" class="com.zgy.service.UserService">
<!-- <property name="userDAO" ref="u" /> -->
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>
</beans>
(3)使用的情况不多,一般自定义属性的值不通过此方式赋值
五、bean的生存范围
Spring中的bean可以有如下几种范围:
1.singleton
单例,不论取多少次,取得的都是一个对象
(1)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- bean也可以使用name来标识 -->
<bean name="u" class="com.zgy.impl.UserDAOImpl">
</bean>
<bean name="userService" class="com.zgy.service.UserService" scope="singleton">
<!-- <property name="userDAO" ref="u" /> -->
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>
</beans>
(2)测试方法:
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zgy.model.User;
import com.zgy.service.UserService;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
UserService userService2 = (UserService) applicationContext.getBean("userService");
System.out.println(userService == userService2);
User u = new User();
u.setName("zhangsan");
u.setPassword("zhangsan");
userService.add(u);
}
}
(3)结果:
true
user saved
2.prototype
每次取得的都是不同的对象
(1)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- bean也可以使用name来标识 -->
<bean name="u" class="com.zgy.impl.UserDAOImpl">
</bean>
<bean name="userService" class="com.zgy.service.UserService" scope="prototype">
<!-- <property name="userDAO" ref="u" /> -->
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>
</beans>
(2)测试方法:
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zgy.model.User;
import com.zgy.service.UserService;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
UserService userService2 = (UserService) applicationContext.getBean("userService");
System.out.println(userService == userService2);
User u = new User();
u.setName("zhangsan");
u.setPassword("zhangsan");
userService.add(u);
}
}
(3)结果:
false
user saved
以下是与web相关的scope,但是与web相关的可以不使用spring来配置
3.request
4.session
5.Application
Spring——IOC(二)