初识 Spring(04)---(bean属性)

简介: bean属性1. id、name都可以表示bean的名字 id:以前属性中不能有特殊字符,现在放特殊字符也没关系 name:属性可以有特殊字符文件目录:代码:配置文件:applicationContext.

bean属性

1. id、name都可以表示bean的名字
 id:以前属性中不能有特殊字符,现在放特殊字符也没关系
 name:属性可以有特殊字符

文件目录:

代码:配置文件:applicationContext.xml

<?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="car" class="com.neudeu.po.Car"></bean>
</beans>
 

Test.java

package com.neuedu.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Object bean = ac.getBean("car");
		Object bean2 = ac.getBean("car");
		System.out.println(bean == bean2);
	}
 
}

Car.java

package com.neudeu.po;

public class Car {
	private String brand;
	private double price;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + "]";
	}
	
	
}

输出:

<bean id="car" class="com.neudeu.po.Car"></bean> 只配置一个 car,

 Object bean = ac.getBean("car");Object bean2 = ac.getBean("car");  无论获取几次但都是同一个对象
 System.out.println(bean == bean2);故输出为 ture

2.scope属性:bean的生存范围
        1.singleton 单例(默认)
        2. proptotype (原型)每次创建新的对象

代码:配置文件:applicationContext.xml

<?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="car" class="com.neudeu.po.Car" scope="prototype"></bean>
</beans>
 

Test.java

package com.neuedu.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Object bean = ac.getBean("car");
		Object bean2 = ac.getBean("car");
		System.out.println(bean == bean2);
	}
 
}

car.java

package com.neudeu.po;

public class Car {
	private String brand;
	private double price;
	
	public Car() {
		System.out.println("Car构造方法");
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + "]";
	}
	
	
}

输出:


修改为 singleton

<?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="car" class="com.neudeu.po.Car" scope="singleton"></bean>
</beans>
 

输出:

    

3.autowire 自动装配(可读性太差)
    1.byName    按名字装配
    2.byType 按类型装配

文件目录:

代码:配置文件:applicationContext.xml

<?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="car" class="com.neudeu.po.Car" scope="singleton"></bean> -->
	<bean id="idCard" class="com.neudeu.po.idCard">
		<property name="cardNo" value="640000200000000011"></property>
	</bean>
	<bean id ="person" class="com.neudeu.po.Person" autowire="byType" >
		
	</bean>
</beans>
 

Test.java

package com.neuedu.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
	}
 
}

idCard.java

package com.neudeu.po;

public class idCard {
	
	public idCard() {
		System.out.println("IdCard...");
	}
	private String CardNo;
	
	public String getCardNo() {
		return CardNo;
	}
	
	public void setCardNo(String cardNo) {
		CardNo = cardNo;
	}
}
 

Person.java

package com.neudeu.po;
import com.neudeu.po.idCard;
import java.util.List;

public class Person {
	private idCard idCard;
	
	private String name;
	private int age;
	
	private List<Car> cars;
	
	public List<Car> getCars() {
		return cars;
	}
	public void setCars(List<Car> cars) {
		this.cars = cars;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public idCard getIdCard() {
		return idCard;
	}
	public Person(){
		System.out.println("Person...");
	}
	public void setIdCard(idCard idCard) {
		System.out.println("setIdCard...");
		this.idCard = idCard;
		
	}
	
	
}

输出:

<bean id ="person" class="com.neudeu.po.Person" autowire="byType" >

autowire="byType" :自动注入(按类型),自动的从 Spring 中找有没有  Person 里需要的 idCard 类型的对象,有点化就自动的将其注入到 Person 的属性里面去

代码:配置文件:applicationContext.xml

<?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="car" class="com.neudeu.po.Car" scope="singleton"></bean> -->
	<bean id="idCard" class="com.neudeu.po.idCard">
		<property name="cardNo" value="640000200000000011"></property>
	</bean>
	
	<bean id="idCard2" class="com.neudeu.po.idCard">
		<property name="cardNo" value="640000200000000012"></property>
	</bean>
	
	<bean id ="person" class="com.neudeu.po.Person" autowire="byName"></bean>
		
	
</beans>
 

输出:autowire="byName" :自动注入(按名字装配)

4. lazy-init    延迟加载
    false : 立即加载(默认)
    true : 用到时加载

代码:配置文件:applicationContext.xml

<?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="car" class="com.neudeu.po.Car" scope="singleton"></bean> -->
	<bean id="idCard" class="com.neudeu.po.idCard">
		<property name="cardNo" value="640000200000000011"></property>
	</bean>
	
	<bean id="idCard2" class="com.neudeu.po.idCard">
		<property name="cardNo" value="640000200000000012"></property>
	</bean>
	
	<bean id ="person" class="com.neudeu.po.Person" autowire="byName" lazy-init="true"></bean>
		
	
</beans>
 

输出:与前文相比,Person 都没有执行(延迟加载)

修改代码:Test.java

package com.neuedu.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Object bean = ac.getBean("person");  //加入代码
		System.out.println(bean);   //加入代码
	}
 
}

输出:只有执行 Object bean = ac.getBean("person"); 后才会执行后续代码

5  init-method="init"    

在容器对这个bean初始化时执行该方法

代码:配置文件:applicationContext.xml

<?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="car" class="com.neudeu.po.Car" scope="singleton"></bean> -->
	<bean id="idCard" class="com.neudeu.po.idCard">
		<property name="cardNo" value="640000200000000011"></property>
	</bean>
	
	<bean id="idCard2" class="com.neudeu.po.idCard">
		<property name="cardNo" value="640000200000000012"></property>
	</bean>
	
	<bean id ="person"  class="com.neudeu.po.Person" autowire="byName" init-method="init">
//修改代码
</bean>
		
	
</beans>
 

Person.java

package com.neudeu.po;
import com.neudeu.po.idCard;
import java.util.List;

public class Person {
	private idCard idCard;
	
	private String name;
	private int age;
	
	private List<Car> cars;
	
	public List<Car> getCars() {
		return cars;
	}
	public void setCars(List<Car> cars) {
		this.cars = cars;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public idCard getIdCard() {
		return idCard;
	}
	public Person(){
		System.out.println("Person...");
	}
	public void setIdCard(idCard idCard) {
		System.out.println("setIdCard...");
		this.idCard = idCard;
		
	} 
	public void init() {
		System.out.println("init...");  //修改代码
	}
	
	
}

输出:bean初始化时执行 init 方法,输出 init...

6. destroy-method="destroy":

容器关闭时执行该方法

代码:配置文件:applicationContext.xml

<?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 ="person"  class="com.neudeu.po.Person" autowire="byName" init-method="init" destroy-method="destroy"></bean>
		
	
</beans>
 

Person.java

package com.neudeu.po;
import com.neudeu.po.idCard;
import java.util.List;

public class Person {
	private idCard idCard;
	
	private String name;
	private int age;
	
	private List<Car> cars;
	
	public List<Car> getCars() {
		return cars;
	}
	public void setCars(List<Car> cars) {
		this.cars = cars;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public idCard getIdCard() {
		return idCard;
	}
	public Person(){
		System.out.println("Person...");
	}
	public void setIdCard(idCard idCard) {
		System.out.println("setIdCard...");
		this.idCard = idCard;
		
	} 
	public void init() {
		System.out.println("init...");
	}
	public void destroy(){
		System.out.println("destroy...");  //修改代码
	}
	
	
}

输出:并没有输出 destory...  要在关闭容器时才会执行

修改代码:Test.java

package com.neuedu.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
	ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");  //修改代码
		Object bean = ac.getBean("person");
		System.out.println(bean);				
		ac.close();
	}
 
}

输出:执行关闭容器,输出 destory...

目录
相关文章
|
1月前
|
XML 安全 Java
|
10天前
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
|
10天前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
|
15天前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
52 6
|
17天前
|
XML Java 数据格式
🌱 深入Spring的心脏:Bean配置的艺术与实践 🌟
本文深入探讨了Spring框架中Bean配置的奥秘,从基本概念到XML配置文件的使用,再到静态工厂方式实例化Bean的详细步骤,通过实际代码示例帮助读者更好地理解和应用Spring的Bean配置。希望对你的Spring开发之旅有所助益。
80 3
|
2月前
|
缓存 Java Spring
实战指南:四种调整 Spring Bean 初始化顺序的方案
本文探讨了如何调整 Spring Boot 中 Bean 的初始化顺序,以满足业务需求。文章通过四种方案进行了详细分析: 1. **方案一 (@Order)**:通过 `@Order` 注解设置 Bean 的初始化顺序,但发现 `@PostConstruct` 会影响顺序。 2. **方案二 (SmartInitializingSingleton)**:在所有单例 Bean 初始化后执行额外的初始化工作,但无法精确控制特定 Bean 的顺序。 3. **方案三 (@DependsOn)**:通过 `@DependsOn` 注解指定 Bean 之间的依赖关系,成功实现顺序控制,但耦合性较高。
实战指南:四种调整 Spring Bean 初始化顺序的方案
|
1月前
|
安全 Java 开发者
Spring容器中的bean是线程安全的吗?
Spring容器中的bean默认为单例模式,多线程环境下若操作共享成员变量,易引发线程安全问题。Spring未对单例bean做线程安全处理,需开发者自行解决。通常,Spring bean(如Controller、Service、Dao)无状态变化,故多为线程安全。若涉及线程安全问题,可通过编码或设置bean作用域为prototype解决。
34 1
|
3月前
|
XML Java 数据格式
Spring从入门到入土(bean的一些子标签及注解的使用)
本文详细介绍了Spring框架中Bean的创建和使用,包括使用XML配置文件中的标签和注解来创建和管理Bean,以及如何通过构造器、Setter方法和属性注入来配置Bean。
85 9
Spring从入门到入土(bean的一些子标签及注解的使用)
|
4月前
|
缓存 安全 Java
Spring框架中Bean是如何加载的?从底层源码入手,详细解读Bean的创建流程
从底层源码入手,通过代码示例,追踪AnnotationConfigApplicationContext加载配置类、启动Spring容器的整个流程,并对IOC、BeanDefinition、PostProcesser等相关概念进行解释
347 24
|
4月前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
283 18