java

tech2025-10-25  4

bean的作用域

类别: singleton:Bean以单例的方式存在,为默认值 prototype:多例,每次从容器中调用bean时,都会返回一个新的实例。

<?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.xsd"> <bean id="userService" class="com.hetl.day01.impl.UserServiceImpl" scope="singleton"> <!--依赖注入,调用属性的set方法--> <property name="name" value="李四"/> </bean> <bean id="userService1" class="com.hetl.day01.impl.UserServiceImpl" scope="prototype"> <!--依赖注入,调用属性的set方法--> <property name="name" value="张三"/> </bean> </beans>

测试:

@Test public void test04(){ ApplicationContext c = new ClassPathXmlApplicationContext("beans1.xml"); //从spring容器获取userService对象 UserService u1 = (UserService) c.getBean("userService"); u1.add(); UserService u2 = (UserService) c.getBean("userService"); u2.add(); UserService u3 = (UserService) c.getBean("userService1"); u1.add(); UserService u4 = (UserService) c.getBean("userService1"); u2.add(); System.out.println(u1); System.out.println(u2); System.out.println(u3); System.out.println(u4); }

输出:

Bean的生命周期:

<?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.xsd"> <bean id ="student" class="com.hetl.day02.pojo.Student" init-method="myInit" destroy-method="myDestroy"> <property name="name" value="zhangsan"/> <property name="gender" value=""/> <property name="id" value="15"/> </bean> </beans> package com.hetl.day02.pojo; import org.springframework.beans.BeansException; import org.springframework.beans.factory.*; public class Student implements BeanNameAware , BeanFactoryAware, InitializingBean, DisposableBean { private String name; private String gender; private Integer id; public String getName() { return name; } public void setName(String name) { System.out.println("2.属性赋值"+name); this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Student() { System.out.println("1.实例化。。。。"); } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", id=" + id + '}'; } public void setBeanName(String s) { System.out.println("3.设置Bean的名字"+s); } public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("4.设置BeanFactory的名字"+beanFactory); } public void afterPropertiesSet() throws Exception { System.out.println("6.属性赋值完成。。。。"); } public void myInit(){ System.out.println("7.自己设置初始化方法"); } public void destroy() throws Exception { System.out.println("9.bean销毁"); } public void myDestroy(){ System.out.println("10.自己的方法销毁"); } } @Test public void test05() throws Exception { ApplicationContext c = new ClassPathXmlApplicationContext("beans1.xml"); //从spring容器获取userService对象 Student s = (Student) c.getBean("student"); System.out.println(s); //关闭容器 c.getClass().getMethod("close").invoke(c); } 1.实例化。。。。 2.属性赋值zhangsan 3.设置Bean的名字student 4.设置BeanFactory的名字org.springframework.beans.factory.support.DefaultListableBeanFactory@6e1ec318: defining beans [student]; root of factory hierarchy 6.属性赋值完成。。。。 7.自己设置初始化方法 Student{name='zhangsan', gender='男', id=15} 9.bean销毁 10.自己的方法销毁

依赖注入的方法:

构造器注入、set注入

<!--构造方法注入属性值 <bean id="student" class="com.hetl.day02.pojo.Student1"> <constructor-arg name="gender" value="女"></constructor-arg> <constructor-arg name="name" value="小雨"></constructor-arg> <constructor-arg name="id" value="15"></constructor-arg> </bean> --> <!--通过索引加类型 给构造方法赋值 <bean id="student" class="com.hetl.day02.pojo.Student1"> <constructor-arg index="1" value="男"></constructor-arg> <constructor-arg index="0" value="小王"></constructor-arg> <constructor-arg index="2" value="12"></constructor-arg> </bean> --> <!--通过set方法注入--> <bean id="student" class="com.hetl.day02.pojo.Student1"> <property name="name" value="xiaotian"></property> <property name="gender" value="zhong"></property> <property name="age" value="12"></property> </bean>

注入:

List注入、set注入、map注入、properties注入 数组注入

<?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.xsd"> <bean id="user" class="com.hetl.day02.pojo.User"> <property name="gender" value=""></property> <property name="name" value="小王"></property> <property name="age" value="16"></property> <property name="animals"> <list> <value>小狗</value> <value>小猪</value> <value>小猫</value> </list> </property> <property name="cars"> <set> <value>自行车</value> <value>自行车</value> <value>山地车</value> <value>三轮车</value> </set> </property> <property name="infos"> <map> <entry key="1" value="周一"></entry> <entry key="2" value="周三"/> </map> </property> <property name="prop"> <props> <prop key="name">jdbc.username.root</prop> <prop key="driver">jdbc.driver</prop> </props> </property> </bean> </beans> import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class User { private String name; private Integer age; private String gender; private List<String> animals; private Set<String> cars; private Map<String,String> infos; private Properties prop; public User() { } public User(String name, Integer age, String gender, List<String> animals, Set<String> cars, Map<String, String> infos, Properties prop) { this.name = name; this.age = age; this.gender = gender; this.animals = animals; this.cars = cars; this.infos = infos; this.prop = prop; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", animals=" + animals + ", cars=" + cars + ", infos=" + infos + ", prop=" + prop + '}'; } public Properties getProp() { return prop; } public void setProp(Properties prop) { this.prop = prop; } public Map<String, String> getInfos() { return infos; } public void setInfos(Map<String, String> infos) { this.infos = infos; } public Set<String> getCars() { return cars; } public void setCars(Set<String> cars) { this.cars = cars; } public List<String> getAnimals() { return animals; } public void setAnimals(List<String> animals) { this.animals = animals; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }

test输出:

@Test public void test07() { ApplicationContext c = new ClassPathXmlApplicationContext("beans2.xml"); //从spring容器获取userService对象 User user = (User) c.getBean("user"); System.out.println(user); } //输出: //User{name='小王', age=16, gender='男', animals=[小狗, 小猪, 小猫], cars=[自行车, 山地车, 三轮车], infos={1=周一, 2=周三}, prop={name=jdbc.username.root, driver=jdbc.driver}}

注解注入

@compinent

import com.hetl.day02.pojo.User; public interface UserService { public void add(User user); } import com.hetl.day02.pojo.User; import com.hetl.day02.service.UserService; import org.springframework.stereotype.Component; @Component() public class UserServiceImpl implements UserService { public void add(User user) { System.out.println("add user:"+user); } } <?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--开启注解--> <context:annotation-config/> <!--扫描注解的位置--> <context:component-scan base-package="com.hetl.day02.service"></context:component-scan> </beans> @Test public void test08() { ApplicationContext c = new ClassPathXmlApplicationContext("beans2.xml"); ApplicationContext c1 = new ClassPathXmlApplicationContext("beans3.xml"); //从spring容器获取userService对象 User user = (User) c.getBean("user"); //如果@component没有配置id,通过类型获取 UserService userService = (UserServiceImpl) c1.getBean(UserServiceImpl.class); //配置id 通过id获取 UserService userService = (UserServiceImpl) c1.getBean("userService"); System.out.println(user); userService.add(user); } //User{name='小王', age=16, gender='男', animals=[小狗, 小猪, 小猫], cars=[自行车, 山地车, 三轮车], infos={1=周一, 2=周三}, prop={name=jdbc.username.root, driver=jdbc.driver}} //add user:User{name='小王', age=16, gender='男', animals=[小狗, 小猪, 小猫], cars=[自行车, 山地车, 三轮车], infos={1=周一, 2=周三}, prop={name=jdbc.username.root, driver=jdbc.driver}}
最新回复(0)