轻量级前端框架助力开发者提升项目效率与性能
531
2022-12-04
Spring中Bean的作用域和自动装配方式
目录Bean的作用域默认配置scope = “singleton”scope = “prototype”Bean的自动装配通过name自动装配通过type自动装配
Bean的作用域
Spring中bean的作用域共有singleton、prototype、request、session、application、websocket六种
其中后四种都是用在Web应用程序中的,主要介绍前两种singleton(单例)和prototype(原型)
Bean的作用域范围为singleton时,所有实例共享一个对象。
Spring的默认配置为scope = “singleton”,以下两种配置的效果是一样的:
默认配置
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:c="http://springframework.org/schema/c" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd"> <bean id = "user" class="indi.stitch.pojo.User" />
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:p="http://springframework.org/schema/p"
xmlns:c="http://springframework.org/schema/c"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
<bean id = "user" class="indi.stitch.pojo.User" />
scope = “singleton”
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:c="http://springframework.org/schema/c" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:p="http://springframework.org/schema/p"
xmlns:c="http://springframework.org/schema/c"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
测试类及输出结果:
import indi.stitch.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("namespace.xml");
User user = context.getBean("user", User.class);
User user2 = context.getBean("user", User.class);
SysXpDTgBsnstem.out.println(user == user2);
}
}
scope = “prototype”
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:c="http://springframework.org/schema/c" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:p="http://springframework.org/schema/p"
xmlns:c="http://springframework.org/schema/c"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
测试类及输出结果:
import indi.stitch.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("namespace.xml");
User user = context.getBean("user", User.class);
User user2 = context.getBean("user", User.class);
System.out.println(user == user2);
}
}
Bean的自动装配
Spring中Bean的自动装配基于autowired标签实现
首先创建实体类People、Cat、Dog,People和Cat、Dog是组合关系,People中定义了依赖于Cat、Dog的属性
People实体类
package indi.stitch.pojo;
public class People {
private Cat cat;
private Dog dog;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
'}';
}
}
Cat实体类
package indi.stitch.pojo;
public class Cat {
public void shout() {
System.out.println("miao~");
}
}
Dog实体类
package indi.stitch.pojo;
public class Dog {
public void shout() {
System.out.println("wang~");
}
}
通过name自动装配
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
测试类及输出结果:
import indi.stitch.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
PeoXpDTgBsnsple people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
}
输出结果
通过type自动装配
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
测试类和结果和上面相同
import indi.stitch.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
}
输出结果
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~