/*****************************************************************************************************/
22:50 2017/10/18 (Spring框架)单例和多例以及依赖注入
/*****************************************************************************************************/
·bean属性中,常用的属性还有scope,其默认值是"singleton".
scope属性:当值是singleton表示单例,当值是prototype表示多例。
这里单例和多例的区别:
(1)、单例:多次用factory.getBean("user",user.class)获得实体类,获得是同一个类。
多例:多次用factory.getBean("user",user.class)获得实体类,获得是多个类。
(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-3.0.xsd">
<!-- 相当于User user = new User() -->
<!-- scope属性:当值是singleton表示单例,当值是prototype表示多例 -->
<bean id = "user" class = "com.wk.model.user" scope = "singleton"></bean>
</beans>
|-:测试类:
package com.wk.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wk.model.user;
public class TestUser {
@Test
public void test01() {
//加载xml配置文件。
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
//得到配置文件中的对象
user u1 = factory.getBean("user",user.class);//运行时类,这样就避免了强制转换
user u2 = factory.getBean("user",user.class);
System.out.println(u1 == u2);
}
}
结果是:ture。
·多例(两次取到的对象不一样):
|-:配置文件:
<?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-3.0.xsd">
<!-- 相当于User user = new User() -->
<!-- scope属性:当值是singleton表示单例,当值是prototype表示多例 -->
<bean id = "user" class = "com.wk.model.user" scope = "prototype"></bean>
</beans>
|-:测试类:
package com.wk.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wk.model.user;
public class TestUser {
@Test
public void test01() {
//加载xml配置文件。
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
//得到配置文件中的对象
user u1 = factory.getBean("user",user.class);//运行时类,这样就避免了强制转换
user u2 = factory.getBean("user",user.class);
System.out.println(u1 == u2);
}
}
结果是false。
不写scope的话默认值是单例。
·判断对象是单例还是多例:
判断一个对象到底是单例还是多例,依据就是:在不同的状态下是否会发生改变。如果说在不同的状态
下会发生改变,就是多例。如果说在不同的状态下不会发生改变,就是单例。
1、若对象中有属性,但是属性不会发生变化,就是单例。
2、一个对象里面没有属性,就是使用单例。
·在各个类中只是声明了一个变量,但没有创建对象,创建对象在配置文件中完成。
·范例:
包层:action、dao、dao(impl)、model、service、service(impl)、test
action:完成与界面层的传值和跳转;负责与页面的传值和跳转。
service: 处理业务逻辑
* 以面向接口编程的形式,较为灵活。
* 1、加密或解密。
* 2、异常处理。
* 3、验证码的问题。
!!!配置文件:
<?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-3.0.xsd">
<!-- 相当于User user = new User() -->
<!-- scope属性:当值是singleton表示单例,当值是prototype表示多例 -->
<bean id = "user" class = "com.wk.model.user" scope = "prototype"></bean>
<bean id = "userDao" class = "com.wk.dao.UserDao" scope = "prototype"></bean>
<bean id = "userService" class = "com.wk.service.UserService">
<!-- 依赖注入:把用到的userDao对象注入到userService层中,
这样就相当于在userService类之中创建了一个userDao对象 -->
<!-- 依赖注入:name属性的值与userService中的属性名称相对应,ref与上面的id属性相对应 -->
<property name = "userDao" ref = "userDao"></property> <!-- ref与上面注入对象的id值对应 -->
</bean>
<bean id = "Action" class = "com.wk.action.Action" scope = "prototype">
<property name="userService" ref = "userService"></property>
</bean>
</beans>
!!!测试类:
package com.wk.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wk.action.Action;
import com.wk.model.user;
public class TestUser {
@Test
public void test01() {
//加载xml配置文件。
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
//得到配置文件中的对象
// user u = (user)factory.getBean("user");//向下转型易出差错
user u1 = factory.getBean("user",user.class);//运行时类,这样就避免了强制转换
// u1.show();
/**
* 判断一个对象到底是单例还是多例,依据就是:在不同的状态下是否会发生改变。如果说在不同的状态
* 下会发生改变,就是多例。如果说在不同的状态下不会发生改变,就是单例。
* 1、若对象中有属性,但是属性不会发生变化,就是单例。
* 2、一个对象里面没有属性,就是使用单例。
*/
user u2 = factory.getBean("user",user.class);
System.out.println(u1 == u2);
}
public void test02() {
//加载xml配置文件
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
Action userAction = factory.getBean("Action", Action.class);
user user = new user(1,"张三","123");
userAction.setUser(user);
userAction.add();
Action userAction2 = factory.getBean("userAction", Action.class);
userAction2.add();
}
}
22:50 2017/10/18 (Spring框架)单例和多例以及依赖注入
/*****************************************************************************************************/
·bean属性中,常用的属性还有scope,其默认值是"singleton".
scope属性:当值是singleton表示单例,当值是prototype表示多例。
这里单例和多例的区别:
(1)、单例:多次用factory.getBean("user",user.class)获得实体类,获得是同一个类。
多例:多次用factory.getBean("user",user.class)获得实体类,获得是多个类。
(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-3.0.xsd">
<!-- 相当于User user = new User() -->
<!-- scope属性:当值是singleton表示单例,当值是prototype表示多例 -->
<bean id = "user" class = "com.wk.model.user" scope = "singleton"></bean>
</beans>
|-:测试类:
package com.wk.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wk.model.user;
public class TestUser {
@Test
public void test01() {
//加载xml配置文件。
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
//得到配置文件中的对象
user u1 = factory.getBean("user",user.class);//运行时类,这样就避免了强制转换
user u2 = factory.getBean("user",user.class);
System.out.println(u1 == u2);
}
}
结果是:ture。
·多例(两次取到的对象不一样):
|-:配置文件:
<?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-3.0.xsd">
<!-- 相当于User user = new User() -->
<!-- scope属性:当值是singleton表示单例,当值是prototype表示多例 -->
<bean id = "user" class = "com.wk.model.user" scope = "prototype"></bean>
</beans>
|-:测试类:
package com.wk.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wk.model.user;
public class TestUser {
@Test
public void test01() {
//加载xml配置文件。
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
//得到配置文件中的对象
user u1 = factory.getBean("user",user.class);//运行时类,这样就避免了强制转换
user u2 = factory.getBean("user",user.class);
System.out.println(u1 == u2);
}
}
结果是false。
不写scope的话默认值是单例。
·判断对象是单例还是多例:
判断一个对象到底是单例还是多例,依据就是:在不同的状态下是否会发生改变。如果说在不同的状态
下会发生改变,就是多例。如果说在不同的状态下不会发生改变,就是单例。
1、若对象中有属性,但是属性不会发生变化,就是单例。
2、一个对象里面没有属性,就是使用单例。
·在各个类中只是声明了一个变量,但没有创建对象,创建对象在配置文件中完成。
·范例:
包层:action、dao、dao(impl)、model、service、service(impl)、test
action:完成与界面层的传值和跳转;负责与页面的传值和跳转。
service: 处理业务逻辑
* 以面向接口编程的形式,较为灵活。
* 1、加密或解密。
* 2、异常处理。
* 3、验证码的问题。
!!!配置文件:
<?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-3.0.xsd">
<!-- 相当于User user = new User() -->
<!-- scope属性:当值是singleton表示单例,当值是prototype表示多例 -->
<bean id = "user" class = "com.wk.model.user" scope = "prototype"></bean>
<bean id = "userDao" class = "com.wk.dao.UserDao" scope = "prototype"></bean>
<bean id = "userService" class = "com.wk.service.UserService">
<!-- 依赖注入:把用到的userDao对象注入到userService层中,
这样就相当于在userService类之中创建了一个userDao对象 -->
<!-- 依赖注入:name属性的值与userService中的属性名称相对应,ref与上面的id属性相对应 -->
<property name = "userDao" ref = "userDao"></property> <!-- ref与上面注入对象的id值对应 -->
</bean>
<bean id = "Action" class = "com.wk.action.Action" scope = "prototype">
<property name="userService" ref = "userService"></property>
</bean>
</beans>
!!!测试类:
package com.wk.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wk.action.Action;
import com.wk.model.user;
public class TestUser {
@Test
public void test01() {
//加载xml配置文件。
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
//得到配置文件中的对象
// user u = (user)factory.getBean("user");//向下转型易出差错
user u1 = factory.getBean("user",user.class);//运行时类,这样就避免了强制转换
// u1.show();
/**
* 判断一个对象到底是单例还是多例,依据就是:在不同的状态下是否会发生改变。如果说在不同的状态
* 下会发生改变,就是多例。如果说在不同的状态下不会发生改变,就是单例。
* 1、若对象中有属性,但是属性不会发生变化,就是单例。
* 2、一个对象里面没有属性,就是使用单例。
*/
user u2 = factory.getBean("user",user.class);
System.out.println(u1 == u2);
}
public void test02() {
//加载xml配置文件
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
Action userAction = factory.getBean("Action", Action.class);
user user = new user(1,"张三","123");
userAction.setUser(user);
userAction.add();
Action userAction2 = factory.getBean("userAction", Action.class);
userAction2.add();
}
}