首页 > 学院 > 开发设计 > 正文

Spring(三、Spring Bean自动装配和注解注入)

2019-11-06 07:30:18
字体:
来源:转载
供稿:网友

1.在xml中进行自动装配 实现类

public class Person {public void write(){ System.out.PRintln("我是一个人");}}public class Student { private int id; private String name; private Person person; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; }}

配置文件

<bean id="person" class="com.iotek.bean.Person"></bean> <bean id="stu" class="com.iotek.bean.Student" autowire="byName"></bean>

测试类

public class Demo { public static void main(String[] args) { // TODO Auto-generated method stub applicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu=context.getBean("stu",Student.class); stu.getPerson().write(); }

spring容器自动装配Bean,需要的参考就是bean的autowire属性,自动装配有3中方式: (1)根据名称自动装配(byName)。因为spring 容器中Bean的名称是唯一的,所以不会产生歧义,推荐使用这种方式。 (2)根据类型自动装配(byType)。在Spring容器中可能会找到多个跟需要注入的bean的类型相同的Bean,所以会产生歧义,Spring容器不知道注入哪个Bean,这时会抛出异常。 (3)根据构造器自动装配(constructor)。 详解(3): a)装配bean构造方法中相对应的ID,如果没有的则在配置文件中手动装配:

<bean id="stu" class="com.iotek.bean.Student" autowire="constructor"> <constructor-arg name="card" ref="card"> </constructor-arg> <constructor-arg name="name" value="aaa"></constructor-arg> </bean>

2.使用注解实现自动装配 (1)使用注解形式装配bean,而不用再配置文件中配置bean (2)Spring默认是关闭注解的 i.必须开启注解 ii.在表头添加context的信息

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

iii.开启

<context:annotation-config></context:annotation-config>

iv.@Autowired:::注解的使用(不用再写set方法)

@Autowired private Person person;

自动检测bean

<context:component-scan base-package="com.iotek.bean"></context:component-scan>

为包下的对象自动生成Bean:[每个类上添加:@Component(“代表bean Id”)]

@Component("student")public class Student {

完整代码示例: 第一步:在配置文件里面进行配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 1.引入约束内容xmlns:context http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" 2.配置spring扫描注解 context:component-scan : 可以扫描到类上面和属性上面的和方法上面的注解 context:annotation-config: 可以扫描只是属性上面的注解 --> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.iotek.bean"></context:component-scan> </beans>

第二步:创建类

//类似于之前xml配置文件 <bean id="person" class="com.iotek.bean.Person">@Component("person")public class Person {public void write(){ System.out.println("我是一个人");}}

第三步:创建测试类

public class Demo { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Person person=context.getBean("person", Person.class); person.write(); }}

Bean管理常用的注解 第一部分注解:用来生成类对象 注解 功能 @Component 生成类对象 @Repository 用于对Dao实现类进行标注 @Service 用于对Service实现类进行标注 @Controller 用于对Controller实现类进行标注 这四个注解的功能是一样的,都是用来生成类的对象,可以使用在类上面

@Controller(value="person")public class Person{}

第二部分注解:做注入的操作,把类对象注入到另一个类中 在此类中声明要注入的对象所在类,作为一个属性,不需要set方法 使用@Autowried进行自动注入(自动装配),根据类对象名称,找到类的对象

@Autowriedprivate Person person;

使用@Resource(name=”值”)实现指定哪个对象注入

@Resource(name="person") private Person person;

示例: 第一步:创建实体类

import org.springframework.stereotype.Component;@Component("person")public class Person {public void write(){ System.out.println("我是一个人");}}import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Component("student")public class Student {@Resource(name="person") private Person person;public void read(){ person.write();}}

第二步:在配置文件里面进行配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 1.引入约束内容xmlns:context http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" 2.配置spring扫描注解 context:component-scan : 可以扫描到类上面和属性上面的和方法上面的注解 context:annotation-config: 可以扫描只是属性上面的注解 --> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.iotek.bean"></context:component-scan> </beans>

第三步:创建测试类

import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.iotek.bean.Person;import com.iotek.bean.Student;public class Demo { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu=context.getBean("student",Student.class); stu.read(); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表