Spring之注解注入bean

1.引入jar包。配置log4j.properties文件

Spring之注解注入bean_第1张图片

2.创建Person类
package com.hy.spring.pojo;

import org.springframework.stereotype.Component;

//使用component的注解方式注入
//@Component("person"),等同于使用依赖注入方式,配置的
@Component("person")
public class Person {
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", mycar=" + mycar + "]";
	}

	private String name;
	private Integer age;
	private Car mycar;
	
	public  Person() {
		super();
		System.out.println("构造方法被调用");
	}
	
	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 Car getMycar() {
		return mycar;
	}

	public void setMycar(Car mycar) {
		this.mycar = mycar;
	}
}

创建Car类

package com.hy.spring.pojo;


public class Car {
	
	private String name;
	private String color;
	
	@Override
	public String toString() {
		return "Car [name=" + name + ", color=" + color + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}	
}

3.配置applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
        <context:component-scan base-package="com.hy.spring.pojo">context:component-scan>
        
beans>
4.编写测试用例
package com.hy.spring.pojo;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class RunWithTest {


	//在bean容器中查找name为person的对象,就是我们之前在Person类中用注解component创建的bean
	@Resource(name="person")
	private Person p;
	
	@Test
	public void test() {
		System.out.println(p);
	}
}

总结

1.其他类级别的注解
@Component("person")   //适用于所有层
@Service("person")     //适用于Service层
@Repository("person")  //适用于持久层
@Controller("person")  //适用于Controller层
2.限制单例或是多例
@Component("person")   //适用于所有层
@Scope(scopeName="singleton")//限制创建单例对象
//@Scope(scopeName="prototype")//限制创建多例对象
3.set方式注入value值
  • 在私有的成员变量注入
    @Value("BMW")
	private String name;
	@Value("Red")
	private String color;
  • 在set方法中注入
    @Value("BMW")
    public void setName(String name) {
		this.name = name;
	}
	@Value("Red")
	public void setColor(String color) {
		this.color = color;
	}	
4.自动装配注解,@Autowired

注意,此处按照对象的类型进行自动装配

    @Autowired
	private Car mycar;

自动装配的例子
1.Person类

package com.hy.spring.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("person")
public class Person {
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", mycar=" + mycar + "]";
	}

	private String name;
	private Integer age;
	
	@Autowired
	private Car mycar;
	
	public  Person() {
		super();
		System.out.println("构造方法被调用");
	}
	
	public String getName() {
		return name;
	}

	@Value("Tom")
	public void setName(String name) {
		this.name = name;
	}

	
	public Integer getAge() {
		return age;
	}

	@Value("18")
	public void setAge(Integer age) {
		this.age = age;
	}

	public Car getMycar() {
		return mycar;
	}

	
	public void setMycar(Car mycar) {
		this.mycar = mycar;
	}
	
}

2.Car类

package com.hy.spring.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("car")
public class Car {
	
	@Value("BMW")
	private String name;
	@Value("Red")
	private String color;
	
	@Override
	public String toString() {
		return "Car [name=" + name + ", color=" + color + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}	
}

3.applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
        <context:component-scan base-package="com.hy.spring.pojo">context:component-scan>
        
beans>

4.测试类

package com.hy.spring.pojo;


import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class RunWithTest {

	@Resource(name="person")
	private Person p;
	
	@Test
	public void test() {
		System.out.println(p);
	}
}

5.运行结果,控制台输出信息:
Spring之注解注入bean_第2张图片
自动装配可能存在的问题,一个类型有多个对象,自动装配不知道装配哪个对象
如:假设applicationContext.xml配置为


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <bean name="car1"  class="com.hy.spring.pojo.Car">
        <property name="name" value="BMW1">property>
       <property name="color" value="red">property>
    bean>
   
   <bean name="car2"  class="com.hy.spring.pojo.Car">
        <property name="name" value="BMW2">property>
       <property name="color" value="blue">property>
    bean>

beans>    

1.可以用@Qualifier(“name”)解决,如下:

    @Autowired
    @Qualifier("car1")
	private Car mycar;

2.可以用@Resource(name=“name”)解决,如下:

    @Resource(name="car1")
	private Car mycar;

你可能感兴趣的:(javaweb,Spring)