学习笔记——Spring的Bean的装配

Spring中装配Bean的方式有两种,一种是通过配置文件来进行装配,另外一种是通过注解的方式来进行装配。但是在实际的开发中并不是简单的通过一种方式来完成的,而是通过这两种的混搭的方式来完成的。


首先应该讲所需要的Jar添加到项目中


一:通过配置文件的方式来对Bean进行装配


配置文件中引入的信息

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" 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">
</beans>



配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" 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="cn.itcast.spring3.demo5.Car"> -->
	<!-- 通过Bean构造方法的注入要用到 <constructor-arg> 标签 -->


	<!-- 通过Bean的属性名来给属性注入相应 的值 -->
	<!-- name 的值为属性的名称 value 值为需要为属性赋的值 -->
	<!-- <constructor-arg name="name" value="宝马" /> -->
	<!-- <constructor-arg name="price" value="1000000" /> -->

	<!-- 通过构造器中属性的下标来给属性注入值(即属性在构造器中的顺序) -->
	<!-- 标签中type 属性表示 该属性的类型 -->
	<!-- <constructor-arg index="0" type="java.lang.String" value="奔驰" /> -->
	<!-- <constructor-arg index="0" value="奔驰" /> -->

	<!-- <constructor-arg index="1" value="2000000" /> -->

	<!--</bean> -->


	<!-- 通过setter 方法的属性注入 -->
	<!-- <bean id="car2" class="cn.itcast.spring3.demo5.Car2"> -->
	<!-- <property>标签中name 就是属性名称,value就是普通属性的值,ref:引用其他的对象 -->
	<!-- <property name="name" value="保时捷" /> -->
	<!-- <property name="price" value="5000000" /> -->
	<!-- </bean> -->

	<!-- 对普通属性的注入 +++++++++++++++++++++++++++++++++++++++++++++++++++ end -->

	<!-- 对对象属性的注入 +++++++++++++++++++++++++++++++++++++++++++++++++++ -->
	<!-- <bean id="person" class="cn.itcast.spring3.demo5.Person"> -->
	<!-- <property name="name" value="哈哈" /> -->
	<!-- <property name="car2" ref="car2"></property> -->
	<!-- </bean> -->
	<!-- <bean id="car2" class="cn.itcast.spring3.demo5.Car2"> -->
	<!-- <property name="name" value="奇瑞" /> -->
	<!-- <property name="price" value="20000" /> -->
	<!-- </bean> -->

	<!-- 名称空间P ++++++++++++++++++ -->
	<!-- <bean id="car2" class="cn.itcast.spring3.demo5.Car2" p:name="baoma" 
		p:price="132" /> -->
	<!-- <bean id="person" class="cn.itcast.spring3.demo5.Person" p:name="doudoudou" 
		p:car2-ref="car2" /> -->
	<!-- 名称空间P ++++++++++++++++++ end -->

	<!-- 对对象属性的注入 +++++++++++++++++++++++++++++++++++++++++++++++++++ end -->

	<!-- SpEL 注入 ************************************************* -->
	<bean id="car2" class="cn.itcast.spring3.demo5.Car2">
		<property name="name" value="#{'大众'}" />
		<property name="price" value="#{'123000'}" />
	</bean>
	<bean id="person" class="cn.itcast.spring3.demo5.Person">
		<!-- <property name="name" value="#{'小伙子'}" /> -->
		<!-- 此处如果获取其他类中的属性的值,首先需要有getter方法,然后通过下边的方式获得 -->
		<!-- 两种方法获取 -->
		<!-- 通过配置文件的id名称.属性名称获取(此处是通过调用getter()方法来获取的) -->
		<!-- <property name="name" value="#{personInfo.name}" /> -->
		<!-- 通过配置文件的 id名称.方法名( 如showName() ) -->
		<property name="name" value="#{personInfo.showName()}" />
		<property name="car2" value="#{car2}" />
	</bean>
	<bean id="personInfo" class="cn.itcast.spring3.demo5.PersonInfo">
		<property name="name" value="大大大" />
	</bean>
	<!-- SpEL 注入 ************************************************* end -->

</beans>


创建Bean

Car 类:

package cn.itcast.spring3.demo5;

// 通过构造器注入Bean属性的测试
public class Car {

	private String name;
	private double price;

	public Car() {
	}

	public Car(String name, double price) {
		super();
		this.name = name;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

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

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

}

Car2 类:
package cn.itcast.spring3.demo5;

public class Car2 {

	private String name;
	private Double price;

	public void setName(String name) {
		this.name = name;
	}

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

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

}

Person类:

package cn.itcast.spring3.demo5;

public class Person {
	private String name;
	private Car2 car2;

	public void setName(String name) {
		this.name = name;
	}

	public void setCar2(Car2 car2) {
		this.car2 = car2;
	}

	public String toString() {
		return "Person [name=" + name + ", car2=" + car2 + "]";
	}

}

PersonInfo 类:

package cn.itcast.spring3.demo5;

public class PersonInfo {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String showName() {
		return name;
	}
}

测试类:

package cn.itcast.spring3.demo5;

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

public class SpringTest5 {

	@Test
	public void demo1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Springdemo5.xml");

		Car car = (Car) applicationContext.getBean("car");

		System.out.println(car);
	}

	@Test
	public void demo2() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Springdemo5.xml");

		Car2 car2 = (Car2) applicationContext.getBean("car2");

		System.out.println(car2);
	}

	@Test
	public void demo3() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Springdemo5.xml");

		Person person = (Person) applicationContext.getBean("person");

		System.out.println(person);
	}

}


二、通过注解来完成Bean的装配:

Spring 2.5 引入了注解方式来定义Bean

@Component 来描述Spring框架中的Bean

Spring的框架中提供了与@Component注解等效的三个注解


@Repository 用于对DAO实现类进行注解

@Service 用于对Service实现类进行注解

@Controller 用于对Controller 实现类进行注解


在没有进行分层来进行的时候,这三个注解的作用是相同的


首先需要清楚,在配置文件中引入的信息的与仅用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" 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">

</beans>


配置文件:

<?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">

	
	<!-- 告诉配置文件扫描哪个包 ++++++++++++++++++++++++++++++++++++++++++++++++ -->
	<!-- 打开我们使用到的注解,但是在仅用注解的方式,可以选择不配置 -->
	<!-- 当用到注解和配置文件混搭的方式来装配Bean的时候用到 -->
	<context:annotation-config><context:annotation-config>
	<context:component-scan base-package="cn.itcast.Spring3.demo1" />
	<!-- 告诉配置文件扫描哪个包 ++++++++++++++++++++++++++++++++++++++++++++++++ end -->
</beans>


UserService 类:

package cn.itcast.Spring3.demo1;

import org.springframework.stereotype.Component;

/**
 * 通过注解的方式来装配Bean
 */

// 由于Component注解中只有一个属性value,所以可以省略
// userService 相当于给这个Bean 起了一个名字
@Component("userService")
// 基于注解的开发需要在配置文件中引入Context 空间,配置去扫描哪些包
public class UserService {

	public void sayHello() {
		System.out.println("Hello Spring Annotetion……");
	}
}

测试类:

package cn.itcast.Spring3.demo1;

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

public class Spring3Test1 {

	@Test
	public void demo1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService) applicationContext.getBean("userService");
		userService.sayHello();
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值