程序员人生 网站导航

Spring――IOC(二)

栏目:框架设计时间:2015-02-04 09:09:18

3、IdName

Spring的配置文件中,beanid属性和name属性可以相互替换。

1

<?xml version="1.0" encoding="UTF⑻"?>

<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⑵.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⑻"?>

<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⑵.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>


4、简单属性注入

可以在Spring的配置文件中配置简单属性的值

范例:

1UserDAOImpl中有两个属性

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⑻"?>

<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⑵.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)使用的情况不多,1般自定义属性的值不通过此方式赋值


5、bean的生存范围

 

Spring中的bean可以有以下几种范围:

1.singleton

单例,不论取多少次,获得的都是1个对象

1

<?xml version="1.0" encoding="UTF⑻"?>

<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⑵.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⑻"?>

<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⑵.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





------分隔线----------------------------
------分隔线----------------------------

最新技术推荐