//配置
<?xml version="1.0" encoding="UTF-8"?>
<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/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "
xmlns="http://www.springframework.org/schema/beans">
<context:component-scan base-package="cn.jy.domain"></context:component-scan>
</beans>
//User类
package cn.lijun.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//<bean name="user" class="cn.lijun.domain.User">
//@Component("user")
//@Service("user")
//@Controller("user")
@Repository("user")
//@Scope(scopeName="singleton")
public class User {
private String name;
private Integer age;
@Autowired
@Qualifier("car")
private Car car;
public User() {
}
public User(String name, Integer age, Car car) {
this.name = name;
this.age = age;
this.car = car;
}
public User(Integer age, Car car) {
this.age = age;
this.car = car;
}
public String getName() {
return name;
}
@Value("QIANG")
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
@Value("22")
public void setAge(Integer age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
// @PostConstruct
// public void init(){
// System.out.println("我是初始化方法");
// }
// @PreDestroy
// public void destory(){
// System.out.println("我是销毁方法");
// }
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
//Car类
package cn.jy.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("car")
public class Car {
private String name;
private String color;
public String getName() {
return name;
}
@Value("法拉利")
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
@Value("呕吐绿" + "")
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
}