Spring框架系列_Spring框架__通过XML方式配置Bean(三)

Spring框架中的Bean 适合于配置比较复杂,需要分离配置文件的情况简化开发,适用于开发时直接用注解标识更加灵活,适用于需要复杂依赖和程序化配置的场景你可以根据项目的需求和团队的规范选择最合适的配置方式。最常见的方式,适合大多数场景使用静态工厂方法创建对象,适用与对实例化过程有特定要求的情况通过实例方法来创建对象,适用于需要工厂类管理实例化的场景实现复杂实例化逻辑,适用与需要控制复杂对象创建的场景避免在应用启动时创建Bean,适用与非必要的单例Bean适合依赖关系较为明确且必须的情况下使用适合较为灵活,可选的依赖关系注入。 阅读详情

Spring配置Bean有两种形式(XML和注解)

本文演示通过XML方式配置Bean

演示案例工程的目录结构:

1、 Bean的配置方式

   通过全类名(反射)的方式

     

  id:标识容器中的bean。id唯一。

  class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参的构造器

2、依赖注入的方式 

    1)属性注入:通过setter方法注入Bean的属性值或依赖的对象

      

      属性注入使用<Property>元素,使用name指定Bean的属性名称,使用value指定Bean的属性的值

  2)构造器注入:通过构造方法注入Bean的属性和值

   首先创建一个Car类

package com.dk.spring.beans;

public class Car {
	private String brand;
	private int speed;
	private String color;
	public int price;
	
	public Car(String brand, int speed, String color,int price) {
		System.out.println("Car...初始化有参构造器");
		this.brand = brand;
		this.speed = speed;
		this.color = color;
		this.price = price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Car [brand=" + brand + ", speed=" + speed + ", color=" + color + ", price=" + price + "]";
	}

}

然后加入配置文件配置节点: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" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="helloworld" class="com.dk.spring.beans.HelloWorld">
		<property name="name" value="Spring"></property>
	</bean>
	
	<bean id="car" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi"></constructor-arg>
		<constructor-arg value="120"></constructor-arg>
		<constructor-arg value="black"></constructor-arg>
		<constructor-arg value="1000"></constructor-arg>
	</bean>

</beans>

编写测试类SpringBeanConfig.java

运行main方法开启Spring容器并使用

package com.dk.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dk.spring.beans.Address;
import com.dk.spring.beans.Car;
import com.dk.spring.beans.OtherPerson;
import com.dk.spring.beans.Person;
import com.dk.spring.beans.RichPerson;

public class SpringBeanConfig {

	public static void main(String[] args) {
		// 1.创建Spring 的IOC的容器对象
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		        
		// 2.从IOC容器中获取bean实例
		Car car = (Car)ctx.getBean(Car.class);
		        
		// 3、调用hello方法
		System.out.println(car);

        }
}

 如果想给指定属性顺序赋值,可以使用index属性

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="helloworld" class="com.dk.spring.beans.HelloWorld">
		<property name="name" value="Spring"></property>
	</bean>

	<bean id="car" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi" index="0"></constructor-arg>
		<constructor-arg value="120" index="1"></constructor-arg>
		<constructor-arg value="black" index="2"></constructor-arg>
		<constructor-arg value="1000" index="3"></constructor-arg>
	</bean>

</beans>

     如果想给指定属性顺序赋值,也可以使用type属性

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="helloworld" class="com.dk.spring.beans.HelloWorld">
		<property name="name" value="Spring"></property>
	</bean>

	<bean id="car" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi"  type="java.lang.String"></constructor-arg>
		<constructor-arg value="120"   type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="1000"  type="int"></constructor-arg>
	</bean>

</beans>

备注:改变参数配置顺序,会影响赋值顺序,【赋值会根据同一类型出现的顺序依次赋值】,如下图所示

所以这种方式注入的话要注意赋值类型顺序。

 

3)工厂方法注入

静态工厂方法注入bean

利用静态工厂方法可以把bean注入到IOC容器中。在XML文件中配置bean时,要指定class的属性为工厂的类;factory-method属性指定工厂类中工厂方法,用于创建bean;constrctor-arg用于给工厂方法传递参数。实例如下:

ExampleBean.java

package com.dk.spring.beans;

public class ExampleBean {
	
	private String name;
	
	public ExampleBean() {

		System.out.println("ExamleBean的无参构造器...");
	}
	
	public ExampleBean(String name) {
		System.out.println("ExamleBean的有参构造器...");
		this.name = name;
	}

	public static ExampleBean getInstance(String name){
		return new ExampleBean(name);
	}

	@Override
	public String toString() {
		return "ExampleBean [name=" + name + "]";
	}

}
<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="helloworld" class="com.dk.spring.beans.HelloWorld">
		<property name="name" value="Spring"></property>
	</bean>

	<!--class要写工厂的类;factory-method要写工厂类中用于创建bean的方法-->
	<!-- 
		使用静态工厂方法创建一个对象
		factory-method属性:指定要调用的静态方法。
	 -->
	<bean id="exampleBean" class="com.dk.spring.beans.ExampleBean" 
	factory-method="getInstance">
		<constructor-arg value="Jack"></constructor-arg>
	</bean>
</beans>

实例工厂方法注入bean

实例化工厂方法注入bean,需要先实例化一个工厂类,然后通过由实例化工厂对象中的一个方法来创建bean,并注入到容器中。 
在 bean 的 factory-bean 属性里指定拥有该工厂方法的 Bean;在 factory-method 属性里指定该工厂方法的名称;使用 construtor-arg 元素为工厂方法传递方法参数。示例如下

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="helloworld" class="com.dk.spring.beans.HelloWorld">
		<property name="name" value="Spring"></property>
	</bean>

    <!-- 使用静态工厂方法创建一个对象 factory-method属性:指定要调用的静态方法。 -->
	<!--class要写工厂的类;factory-method要写工厂类中用于创建bean的方法 -->
	<bean id="exampleBean" class="com.dk.spring.beans.ExampleBean"
		factory-method="getInstance">
		<constructor-arg value="Jack"></constructor-arg>
	</bean>
	

	<!-- 使用实例工厂方法创建一个对象 factory-bean属性:要调用的对象的id。 factory-method属性:要调用的方法。 -->
	<bean id="calendar" class="java.util.GregorianCalendar" />
	<bean id="time" factory-bean="calendar" factory-method="getTime" />
	
	 <bean id="carFactory" class="com.dk.spring.beans.CarFactory"></bean>
	 <bean id="car" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="baoma"></constructor-arg>
        <constructor-arg value="102"></constructor-arg>
        <constructor-arg value="baoma"></constructor-arg>
        <constructor-arg value="100000"></constructor-arg>
    </bean>

</beans>

CarFactory.java

package com.dk.spring.beans;

public class CarFactory {

	public Car getCar(String brand, int speed, String color,int price){
		return new Car(brand, speed, color, price);
	}

}

配置bean的属性时可以 使用ref引用其他配置好的bean对象,ref的值为引用bean的id

构造器方式<constructor-arg  ref="car"></constructor-arg>

属性注入<property name="name"  ref=""></property>

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="helloworld" class="com.dk.spring.beans.HelloWorld">
		<property name="name" value="Spring"></property>
	</bean>

    <!-- 使用静态工厂方法创建一个对象 factory-method属性:指定要调用的静态方法。 -->
	<!--class要写工厂的类;factory-method要写工厂类中用于创建bean的方法 -->
	<bean id="exampleBean" class="com.dk.spring.beans.ExampleBean"
		factory-method="getInstance2">
		<constructor-arg  ref="car"></constructor-arg>
	</bean>
	

	<!-- 使用实例工厂方法创建一个对象 factory-bean属性:要调用的对象的id。 factory-method属性:要调用的方法。 -->
	<bean id="calendar" class="java.util.GregorianCalendar" />
	<bean id="time" factory-bean="calendar" factory-method="getTime" />
	
	 <bean id="carFactory" class="com.dk.spring.beans.CarFactory"></bean>
	 <bean id="car" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="baoma"></constructor-arg>
        <constructor-arg value="102"></constructor-arg>
        <constructor-arg value="baoma"></constructor-arg>
        <constructor-arg value="100000"></constructor-arg>
    </bean>

</beans>

 

package com.dk.spring.beans;

public class ExampleBean {
	
	private String name;
	private Car car;
	
	public ExampleBean(Car car) {
		System.out.println("ExamleBean的有参构造器...");
		this.car = car;
	}
	public static ExampleBean getInstance2(Car car){
		return new ExampleBean(car);
	}
	
	public ExampleBean() {

		System.out.println("ExamleBean的无参构造器...");
	}
	
	public ExampleBean(String name) {
		System.out.println("ExamleBean的有参构造器...");
		this.name = name;
	}

	public static ExampleBean getInstance(String name){
		return new ExampleBean(name);
	}

	@Override
	public String toString() {
		return "ExampleBean [name=" + name + ", car=" + car + "]";
	}

}

 工厂方法方式在实际的项目中很少使用,使用最多的是属性注入、构造器注入。

3、引用其他Bean

  1)引用外部Bean

          使用 property的ref属性建立Bean之间的引用关系

package com.dk.spring.beans;

public class Person {

	private String name;
	private int age;
	private Car car;
	
	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 Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
		
}
<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="car" class="com.dk.spring.beans.Car">
	    <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
	    <constructor-arg value="120" type="int"></constructor-arg>
	    <constructor-arg value="black" type="java.lang.String"></constructor-arg>
	    <constructor-arg value="100000"></constructor-arg>
	</bean>
	
	<bean id="person" class="com.dk.spring.beans.Person">
	    <property name="name" value="dk"></property>
	    <property name="age" value="32"></property>
	    <!-- 使用 property的ref属性建立Bean之间的引用关系-->
	    <property name="car" ref="car"></property>
	</bean>

</beans>

    2)引用内部Bean

     因为id是为了让外部引用,而内部Bean内部只用一次,不能被外部引用,所以不写id。

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="person" class="com.dk.spring.beans.Person">
		<property name="name" value="dk"></property>
		<property name="age" value="32"></property>
		<!-- 内部Bean,不能被外部引用,只能在内部使用 -->
		<property name="car">
			<bean class="com.dk.spring.beans.Car">
				<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
				<constructor-arg value="120" type="int"></constructor-arg>
				<constructor-arg value="black" type="java.lang.String"></constructor-arg>
				<constructor-arg value="100000"></constructor-arg>
			</bean>
		</property>
	</bean>
</beans>

 4、级联属性

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    
	<bean id="person" class="com.dk.spring.beans.Person">
	    <property name="name" value="dk"></property>
	    <property name="age" value="32"></property>
	    <property name="car" ref="car"></property>
	    <!-- 级联属性赋值 -->
	    <property name="car.price" value="30000"></property>
	</bean>
	
    <bean id="car" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi" index="0"></constructor-arg>
		<constructor-arg value="120" index="1"></constructor-arg>
		<constructor-arg value="black" index="2"></constructor-arg>
		<constructor-arg value="1000" index="3"></constructor-arg>
	</bean>
	
</beans>

 注意:属性想用car.price的级联,首先car必须要先初始化,否则会有异常

 4、集合属性

  当我们的Bean中有集合元素的时候,Spring中可以通过一组内置的xml标签(<list>、<set>、<map>)来配置集合属性。

  1)List集合:

     创建RichPerson类有一个cars属性,并且RichPerson类cars属性是一个List<car>,即一个人拥有多辆车。

package com.dk.spring.beans;

import java.util.List;
import java.util.Map;

public class RichPerson {
    private String name;
    private int age;
    private List<Car> cars;
    private Map<String,Car> cars2;
    
	//为上面的4个属性设置getter和setter方法
    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 List<Car> getCars() {
        return cars;
    }
    public void setCars(List<Car> cars) {
        this.cars = cars;
    }
	public Map<String,Car> getCars2() {
		return cars2;
	}
	public void setCars2(Map<String,Car> cars2) {
		this.cars2 = cars2;
	}
    
}

 配置文件修改如下:

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    
	<bean id="car1" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
		<constructor-arg value="120" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="1000" type="int"></constructor-arg>
	</bean>
	<bean id="car2" class="com.dk.spring.beans.Car">
		<constructor-arg value="Ford" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="1000" index="3"></constructor-arg>
	</bean>
	<bean id="car3" class="com.dk.spring.beans.Car">
		<constructor-arg value="Toyota" type="java.lang.String"></constructor-arg>
		<constructor-arg value="95" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="1000" type="int"></constructor-arg>
	</bean>
	<bean id="richPerson" class="com.dk.spring.beans.RichPerson">
		<property name="name" value="dk"></property>
		<property name="age" value="32"></property>
		<!-- 集合属性(List),使用ref来配置子节点信息 -->
		<property name="cars">
			<list>
				<ref bean="car1"></ref>
				<ref bean="car2"></ref>
				<ref bean="car3"></ref>
			</list>
		</property>
	</bean>
	
</beans>

  2)Map集合:

 如果是<map>集合的话,只需要把list换成map即可。

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    
	<bean id="car1" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
		<constructor-arg value="120" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="1000" type="int"></constructor-arg>
	</bean>
	<bean id="car2" class="com.dk.spring.beans.Car">
		<constructor-arg value="Ford" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="1000" index="3"></constructor-arg>
	</bean>
	<bean id="car3" class="com.dk.spring.beans.Car">
		<constructor-arg value="Toyota" type="java.lang.String"></constructor-arg>
		<constructor-arg value="95" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="1000" type="int"></constructor-arg>
	</bean>
	<bean id="richPerson" class="com.dk.spring.beans.RichPerson">
		<property name="name" value="dk"></property>
		<property name="age" value="32"></property>
		<!-- 集合属性(List),使用ref来配置子节点信息 -->
		<property name="cars">
			<list>
				<ref bean="car1"></ref>
				<ref bean="car2"></ref>
				<ref bean="car3"></ref>
			</list>
		</property>
		
		<!-- 集合属性(Map),使用entry来配置map的子节点信息 -->
		<property name="cars2">
			<map>
				<entry key="A" value-ref="car1"></entry>
				<entry key="B" value-ref="car2"></entry>
			</map>
		</property>
	</bean>
	
</beans>

3) 现在这么多集合Bean能不能抽出来,以供多个Bean进行引用,那么就需要使用util

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <bean id="car1" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
		<constructor-arg value="120" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean>
	<bean id="car2" class="com.dk.spring.beans.Car">
		<constructor-arg value="Ford" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean>
	<bean id="car3" class="com.dk.spring.beans.Car">
		<constructor-arg value="Toyota" type="java.lang.String"></constructor-arg>
		<constructor-arg value="95" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean> 
	
	<bean id="richPerson" class="com.dk.spring.beans.RichPerson">
		<property name="name" value="dk"></property>
		<property name="age" value="32"></property>
		<!-- 引用util -->
		<property name="cars" ref="cars"></property>
	</bean> 

	
	
	<util:list id="cars">
		<ref bean="car1"></ref>
		<ref bean="car2"></ref>
	</util:list> 

	
</beans>

4)使用p命名空间 

 Sping从2.5版本开始引入一个新的P的命名空间,通过使用P命名空间后,使得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" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <bean id="car1" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
		<constructor-arg value="120" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean>
	<bean id="car2" class="com.dk.spring.beans.Car">
		<constructor-arg value="Ford" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean>
	<bean id="car3" class="com.dk.spring.beans.Car">
		<constructor-arg value="Toyota" type="java.lang.String"></constructor-arg>
		<constructor-arg value="95" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean> 	
	
	<util:list id="cars">
		<ref bean="car1"></ref>
		<ref bean="car2"></ref>
	</util:list> 
	
	<!-- 通过P命名空间为Bean属性赋值,相对于传统的配置方式更加简洁 -->
	
	<bean id="richPerson" class="com.dk.spring.beans.RichPerson" p:name="dk"
		p:age="25" p:cars-ref="cars">
	</bean> 


</beans>

 通过C名称空间来简化我们构造方法的属性赋值 

package com.dk.spring.beans;

import java.util.List;
import java.util.Map;

public class RichPerson {
    private String name;
    private int age;
    private List<Car> cars;
    private Map<String,Car> cars2;
    
    //构造函数(C命名空间使用)
    public RichPerson(String name, int age, List<Car> cars) {
		System.out.println("RichPerson有参构造器初始化。。。。");
		this.name = name;
		this.age = age;
		this.cars = cars;
	}
    
	//为上面的4个属性设置getter和setter方法
    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 List<Car> getCars() {
        return cars;
    }
    public void setCars(List<Car> cars) {
        this.cars = cars;
    }
	public Map<String,Car> getCars2() {
		return cars2;
	}
	public void setCars2(Map<String,Car> cars2) {
		this.cars2 = cars2;
	}
    
}
<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <bean id="car1" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
		<constructor-arg value="120" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean>
	<bean id="car2" class="com.dk.spring.beans.Car">
		<constructor-arg value="Ford" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean>
	<bean id="car3" class="com.dk.spring.beans.Car">
		<constructor-arg value="Toyota" type="java.lang.String"></constructor-arg>
		<constructor-arg value="95" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean> 	
	
	<util:list id="cars">
		<ref bean="car1"></ref>
		<ref bean="car2"></ref>
	</util:list> 
	
	<!-- 通过C名称空间来简化我们构造方法的属性赋值 -->
	
	<bean id="richPerson" class="com.dk.spring.beans.RichPerson" c:name="dk"
		c:age="25" c:cars-ref="cars">
	</bean> 



</beans>

 5、使用autowire属性进行Bean的自动装配

当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property ref=“”>标签的形式。但是对于大型项目这种方式其实是不可取的。

package com.dk.spring.beans;

public class Person {

	private String name;
	private int age;
	private Car car;
	
	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 Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
		
}
<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- 使用autowire属性进行Bean的自动装配 -->
	<!-- 
	     大型项目中我们一般使用autowire属性自动装备Bean,autowire属性有两个值:byName和byType
	        byName:根据Bean的名字和当前的Bean的setter属性名称进行装配 若没有,则为null
	        byType:根据Bean的类型进行装配,若有1个以上的同一类型,则抛异常
	-->
	<bean id="car" class="com.dk.spring.beans.Car">
		<constructor-arg value="Toyota" type="java.lang.String"></constructor-arg>
		<constructor-arg value="95" type="int"></constructor-arg>
		<constructor-arg value="black" type="java.lang.String"></constructor-arg>
		<constructor-arg value="100" type="int"></constructor-arg>
	</bean>
	<bean id="person" class="com.dk.spring.beans.Person" autowire="byName">
	     <property name="name" value="dk"></property>
	     <property name="age" value="32"></property>
	</bean>

</beans>

自动装配的特点:

   1)只有引用类型才可以自动装配。

   2)autowire属性是在bean的级别上,一旦指定,当前bean的所有引用类型都必须使用自动装配。

     3)byType和byName的只能选择其一。

 byType:异常情况演示

 byName:

异常情况演示: 

 

6、配置上的继承(parent属性) 

当我们有两个或者以上的同一个类型的Bean做实例化的时候,我们可以将一些公共的部分提取成通用Bean。这样其他的Bean继承它即可以了。

package com.dk.spring.beans;

public class Address {

	private String provice;
	private String city;
	private String street;
	
	public String getProvice() {
		return provice;
	}
	public void setProvice(String provice) {
		this.provice = provice;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	
	@Override
	public String toString() {
		return "Address [provice=" + provice + ", city=" + city + ", street=" + street + "]";
	}
	
}
<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	
	<bean id="address1" class="com.dk.spring.beans.Address" p:provice="Hebei"
		p:city="LangFang" p:street="YingBing">
	</bean>
	<bean id="address2" class="com.dk.spring.beans.Address" p:provice="Hebei"
		p:city="LangFang" p:street="YanLing">
	</bean>
	<bean id="address3" class="com.dk.spring.beans.Address" p:provice="Hebei"
		p:city="LangFang" p:street="YanShun">
	</bean>  

</beans>

以上Bean中用的都是address这个类,都是省市街道的赋值,只是在街道上每个不一样,所以我们完全可以把通用的提出来。

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- 	
	<bean id="address1" class="com.dk.spring.beans.Address" p:provice="Hebei"
		p:city="LangFang" p:street="YingBing">
	</bean>
	<bean id="address2" class="com.dk.spring.beans.Address" p:provice="Hebei"
		p:city="LangFang" p:street="YanLing">
	</bean>
	<bean id="address3" class="com.dk.spring.beans.Address" p:provice="Hebei"
		p:city="LangFang" p:street="YanShun">
	</bean>  
	-->
	
	<bean id="address" class="com.dk.spring.beans.Address" p:provice="Hebei"
		p:city="LangFang" p:street="YingBing" scope="prototype">
	</bean>
	<bean id="address1" parent="address" p:street="YanLing">
	</bean>
	<bean id="address2" parent="address" p:street="YanShun">
	</bean>

</beans>

 7、Bean的作用域(scope属性) 

输出结果为:true

  说明默认为单例模式的,主要看scope属性值。

  使用Bean的scope属性来配置Bean的作用域的,scope有两个重要的属性值。

    singleton:默认值,容器初始化时创建Bean实例,在整个容器的生命周期内只创建一个Bean,单例的。

    prototype:原型的,容器初始化时不创建Bean的实例,而在每次请求时都创建一个新的Bean的实例,并返回。

8、使用外部属性文件(property-placeholder 属性占位符)

新建一个db.properties文件

user=root
password=1234
driverClass=com.mysql.jdbc
jdbcUrl=jdbc:mysql:///test

 MyDataSource文件:

package com.dk.spring;

public class MyDataSource {
    private String user;
    private String password;
    private String diverClass;
    private String jdbcUrl;
    
    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDiverClass() {
        return diverClass;
    }

    public void setDiverClass(String diverClass) {
        this.diverClass = diverClass;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    @Override
    public String toString() {
        return "MyDataSource{" +
                "user='" + user + '\'' +
                ", password='" + password + '\'' +
                ", diverClass='" + diverClass + '\'' +
                ", jdbcUrl='" + jdbcUrl + '\'' +
                '}';
    }
}

配置上属性占位符是文件的地址,并在使用的过程中使用${}的格式取值

<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- 导入属性文件 -->
	<context:property-placeholder location="db.properties"></context:property-placeholder>
	<bean id="dataSource" class="com.dk.spring.MyDataSource">
		<!-- 使用外部属性文件的属性值 -->
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
		<property name="diverClass" value="${driverClass}"></property>
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
	</bean>
	

</beans>

9、Spring表达式SPEL表达式 

package com.dk.spring.beans;

public class OtherPerson {
	private Car car;
	private String city;
	private String info;
	private int yaowei;
	
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public int getYaowei() {
		return yaowei;
	}
	public void setYaowei(int yaowei) {
		this.yaowei = yaowei;
	}
	
	@Override
	public String toString() {
		return "OtherPerson [car=" + car + ", city=" + city + ", info=" + info + ", yaowei=" + yaowei + "]";
	}
	
}
<?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"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="car" class="com.dk.spring.beans.Car">
		<constructor-arg value="Audi" index="0"></constructor-arg>
		<constructor-arg value="120" index="1"></constructor-arg>
		<constructor-arg value="black" index="2"></constructor-arg>
		<constructor-arg value="1000" index="3"></constructor-arg>
	</bean>
	
	<bean id="address" class="com.dk.spring.beans.Address" p:provice="Hebei"
		p:city="LangFang" p:street="YingBing">
	</bean>
	
	<!-- Spring表达式SPEL表达式 -->
	<bean id="otherPerson" class="com.dk.spring.beans.OtherPerson">
		<!-- 使用SpEL 来应用其他的Bean -->
		<property name="car" value="#{car}"></property>
		<!-- 使用SpEL 来应用其他的Bean的属性 -->
		<property name="city" value="#{address.city}"></property>
		<!-- 使用SpEL 中使用运算符 -->
		<property name="info" value="#{car.price > 30000 ? '金领' : '白领'}"></property>
		<!-- 使用SpEL 引用类的静态属性 -->
		<property name="yaowei" value="#{T(java.lang.Math).PI * 80}"></property>
	</bean>

</beans>

 10、Spring中Bean的生命周期 

       1)创建Bean的实例

  2)为Bean的属性设置值和对其他Bean的引用

  3)Bean后置处理器postProcessBeforeInitialization方法

  4)调用Bean的初始化方法(init-method)

  5)Bean后置处理器的postProcessAfterInitialization方法

  6)Bean正常使用

  7)当容器关闭时,调用Bean的销毁方法(destroy-method)

  实例:

package com.dk.spring.life;

public class Car {
    public Car() {
        System.out.println("构造器……");
    }
    private String brand;

    public void setBrand(String brand) {
        System.out.println("设置属性……");
        this.brand = brand;
    }
    public void init(){
        System.out.println("初始化……");
    }
    public void destroy(){
        System.out.println("销毁……");
    }
}

MyBeanProcessor必须实现BeanPostProcessor接口的两个方法:

package com.dk.spring.life;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("postProcessBeforeInitialization……");
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("postProcessAfterInitialization……");
        return o;
    }
}

配置文件:

<?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"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	
	
	<bean id="car" class="com.dk.spring.life.Car" p:brand="Audi" init-method="init"
	destroy-method="destroy"></bean>
	<!-- 配置Bean的处理器 -->
	<bean class="com.dk.spring.life.MyBeanProcessor"></bean>
	
	
</beans>

测试类:

package com.dk.spring.life;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanLifeTest {

	public static void main(String[] args) {
		// 由于ApplicationContext没有close方法,所以要使用它下面子接口ConfigurableApplicationContext
		ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-beanLife.xml");
		Car car = (Car) ctx.getBean("car");
		System.out.println("使用Bean:" + car.toString());
		// 关闭IOC容器
		ctx.close();
	}

}

最终运行结果验证了上面的spring中bean的生命周期: 

 

TwinCAT 3 实战指南:从系统配置到高级运动控制 本文详细介绍了TwinCAT 3工业自动化控制平台的实战应用,从系统配置、PLC编程到EtherCAT总线配置和高级运动控制。通过倍福(Beckhoff)的TwinCAT 3平台,工程师可以高效实现复杂的工业自动化任务,包括运动控制、PLC编程等。文章还提供了常见问题排查和性能优化技巧,帮助用户提升系统稳定性和实时性。 阅读详情

相关推荐

WSL2下用QEMU模拟ARM开发板:从编译uboot到调试全流程(附常见错误解决)

本文详细介绍了在WSL2环境下使用QEMU模拟ARM开发板的完整流程,包括uboot编译、内核调试等关键步骤。通过QEMU模拟vexpress-a9开发板,开发者可以高效搭建嵌入式开发环境,解决常见错误并提升开发效率。

solidity8miner的博客 286

【Spring学习05】四种xml配置注入方式

平常应用Spring开发中,用得最多的是容器。spring容器帮我们实例化对象并且注入到需要该对象的类中。 spring有多种依赖注入的形式,下面仅介绍spring通过xml进行IOC配置的方式:一、Setter注入set注入是最简单常用的注入方式,《【Spring学习03】Spring简单入门实例 》中就是用的set注入。 Order类中需要用到NotifyService对象,于是定义了一个p

程序老兵的博客 1万+

绕线机的制作与研究

本文详细阐述了绕线机的制作过程,涵盖从设计规划到实际制作的各个环节。通过对绕线机工作原理的深入分析,确定了关键技术参数,并依此完成机械结构与控制系统的设计。在制作阶段,运用多种加工工艺完成零件制造与设备组装。经测试,自制绕线机性能达到预期,能够满足特定线圈绕制需求,为相关领域的生产实践与技术研究提供了参考。

道亦无名 1437

Spring系列二:基于XML配置bean 上

Spring系列二: 基于注解配置bean

你好, 我在学java的博客 3535

Lakes.AERMOD.View.v8.9.0 1CD大气扩散模型软件包

Lakes.AERMOD.View.v8.9.0 1CD大气扩散模型软件包 Lakes Environmental ARTM View v1.4.2 1CD放射物扩散模拟模型 Aermod View是一套完整的、功能强大的大气扩散模型软件包,并将美国EPA指定的大气扩散模型(如下)完美的集成成为一个综合的操作平 台: - AERMOD 电话TEL:18980583122  客服 QQ:11

yyii0000的博客 4001

从原理到实战:ARS548 4D毫米波雷达数据处理与多模态融合全解析

本文深入解析了4D毫米波雷达ARS548的工作原理与数据处理全流程。从FMCW测距测速、MIMO虚拟天线阵列到点云生成的三步FFT处理,提供了详尽的Python代码实现。文章进一步探讨了与激光雷达、相机的多模态时空对齐与融合策略,并指导如何构建用于自动驾驶感知训练的多模态标注数据集,为相关算法工程师提供了从理论到实践的完整指南。

weixin_29161719的博客 366

Spring底层原理之bean的加载方式一 用XML方式声明bean 自定义bean及加载第三方bean 2024详解

我们现在声明的bean都是自己手写的。使用 XML 方式声明 Spring Bean 是 Spring 框架的一种传统配置方式。这种方法通过在 XML 文件中定义 bean 及其依赖关系,来组装应用程序的各个组件。以下是详细介绍如何使用 XML 方式声明 bean

多多的博客 11万+

DRAM刷新refresh相关知识归类-基础小知识(三)

RAM每一位数据都是通过芯片内的一个小电容充电(或低电平)存储的。但随着时间的推移,电容逐渐失去电荷。为避免丢失存储的数据,必须定期刷新以将电荷(如果存在)恢复到其原始级别。此刷新过程包括读取每个位的值,然后将其写回。在此“刷新”期间,内存被占用,无法执行加载或存储位等正常操作。知道了应该这么做,但怎么去做以及有什么办法更有效率去做,就是接下来要整理的内容。

Paul 安的博客 1万+

Spring 如何配置 bean (XML 方式)

Java Bean 如何配置配置到 spring 容器中 基于 XML 的配置 通过生成 applicationContext.xml 文件,声明命名空间来配置 Java Bean,XML 基本格式如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-i

qq_37391214的博客 1万+

Rockchip开发系列 - 3.4.GPIO扩展芯片AW95xx和PCA953X(2)

Rockchip开发系列 - 3.3.GPIO扩展芯片AW95xx和PCA953X(2)

fulinux的博客 1002

轴承故障诊断领域的论文到底有多水

这个是最让人困惑的,就好像你说自己证明了某个方法更好,但是你却没有交代自己怎么做的,或者说你交代的过程有外行话或者逻辑上的缺陷,但是你就这么给出个结果完事了。由于自己没有足够大的学术交流圈子,为了尽量避免误判,我在原来13篇论文的基础上又读了18篇,这些论文至少都出自中文核心,有一些还有EI的title,事实上近几年能找到的文章也不算多,这些基本能说明问题了。当然,还有更多细节,比如说损失函数你得交代吧,模型超参数得介绍全吧,至少数据集是哪个工况下的得说吧,当你想复现这些论文,关注点在这些地方的时候。

weixin_50642818的博客 3052

spring 之基于XML的方式配置 Bean

Spring 通过 IOC 容器的方式管理 Bean。在本篇,将通过 xml 配置的方式介绍如何配置 Bean。 1. 常见的 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" xmlns:p=.

weixin_44245828的博客 1425

Flatten 层的深度理解与运用

Flatten 层的深度理解与运用

你要拥有一个你喜欢并且热爱的人生 7032

【Spring教程】配置Bean的三种方式

首先,创建两个实体类,Hello类和Address类public class Hello { private String name; private Address address; public Hello() {} public Hello(String name) { this.name=name;

程序员云帆哥的博客 1万+

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

基础框架 Spring框架bean注入的几种方式

基础框架 Spring框架bean注入的几种方式 Bean注入的方式有两种 XML配置:set注入、构造函数注入,P标签,静态工厂方法与实例工厂方法; 注解方式: @Autowired,@Resource,@Required。 案例 /** * 书 * * @author zrj * @date 2020/12/16 * @since V1.0 **/ public class Book { private String bookName; private String b

靖节先生的博客 1775

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[spring学习]3、基于xml配置bean的常见方式

在这篇文章中,讲解了一些基于xml配置bean的常用方式,当然,方式还有很多,这里只说明了一些,上面的只有配置,就单纯看的化肯定会有点模糊,大家可以自己创建一个测试类来对每种配置进行测试。篇幅有限,其他的配置方法在下一篇文章中进行说明。............

秃头披风侠的博客 5829

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Spring容器Bean之XML配置方式

我们采用xml配置文件的方式对bean进行声明和管理,每一个bean标签都代表着需要被创建的对象并通过property标签可以为该类注入其他依赖对象,通过这种方式Spring容器就可以成功知道我们需要创建那些bean实例。

学习笔记,人工智能,教程,技术分享! 1700

Spring系列二:基于XML配置bean 下

Spring系列二:基于XML配置bean 下

你好, 我在学java的博客 985

spring 的 bean 配置的几种方式

Spring 提供了多种配置BeanXML 配置:传统的方式,适用于对 XML 更熟悉的开发者,或者在大型项目中集中管理配置。基于注解的配置:更加简洁和直观,适用于现代 Java 开发,减少了冗长的 XML 配置文件。基于 Java 配置的方式:提供了类型安全和更强的灵活性,适合需要复杂配置逻辑的场景。Spring Boot 自动配置:大大简化了 Spring 应用的配置过程,遵循“约定优于配置”原则,非常适合快速开发。

Flying_Fish_roe的博客 2084

Spring Bean、XML方式Bean配置、Bean实例化配置、Bean注入

自定义标签:就是需要额外引入其他命名空间约束,并通过前缀引用的标签,例如 标签并且xmlns:context="http://www.springframework.org/schema/context"内部维护的标签(此内部的约束在对应的jar中存在,所以一定要导入坐标),我们都叫做自定义标签Spring的自定义标签需要引入外部的命名空间,并为外部的命名空间指定前缀,使用 形式的标签,称之为自定义标签如下所示。

weixin_51351637的博客 2764

Spring系统学习 -Spring IOC 的XML管理Bean之bean的获取、依赖注入值的方式

在Spring框架中,XML配置是最传统和最常见的方式之一,用于管理Bean的创建、依赖注入和生命周期等。这个在Spring中我们使用算是常用的,我们需要根据Spring的基于XML管理Bean了解相关Spring中常用的获取bean的方式、依赖注入值的几种方式等等。

好看的皮囊千篇一律,有趣的灵魂万里挑一。 1119

Spring Bean的三种配置方式

目录 一、传统的XML配置方式 二、基于java注解的配置 三、基于类的Java Config 正文 Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> <groupId>org.springframework</groupId> <artifactId>spring-cor

publicman_的博客 6467

Spring Bean的装配方式 第1关:基于 XML 的 Bean 装配

基于 XML 的 Bean 装配

于建章的博客 2279

Spring中xml配置bean(三)

**一.自动装配bean: autowire 1.Byname:通过get和set方法来进行bean的装配 2.ByType:通过类型进行bean的装配,如果在此文件中存在属性的类型超过一个的时候,使用此方法会出现异常** <bean id="address2" class="zhang.spring.enities.autowire.Address" p:city="beijing" p:...

qq_43257103的博客 2109

Spring配置Bean的几种方式

​ Spring容器提供了三种装配bean的方式,所谓装配就是指将bean配置在spring容器中,并能够在类与类之间提供依赖关系。 一、隐式Bean发现机制和自动装配 ​ Spring从两个角度来实现自动化装配:组件扫描(Spring自动发现应用中上下文所需要的创建的Bean),自动装配(Spring自动满足Bean之间的依赖) 使用Component注解将普通类配置成Bean @Com...

算云烟 4907

Spring框架几种创建bean的方式

    Spring框架下,Bean的创建和装配非常的灵活,提供了三种主要的方式,并且相互见可以互相看见,也就是你可以随意地采用你喜欢且合适的方式创建Bean,而不用担心他们之间的兼容问题。    一、使用XML显示配置Bean        在Spring框架最初的是,XML是最主要的配置方式。在XML中创建Bean需要使用&lt;bean&gt;元素,例如            &lt;bea...

懒散的奋斗者 7409
上一篇: Spring框架系列_Spring框架--Spring容器(二)
下一篇: 设计模式篇—常用设计模式—23种设计模式简介
Coder_Boy_
博客等级 码龄10年 2804粉丝 819原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Boy_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值