java 注解属性_Java Spring-注解进行属性注入

2017-11-06 21:19:43

一、Spring的注解装配Bean

Spring2.5 引入使用注解去定义Bean

@Component 描述Spring框架中Bean

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

@Repository 用于对DAO实现类进行标注(dao层)

@Service 用于对Service实现类进行标注(service层)

@Controller 用于对Controller实现类进行标注(web层)

//因为只有一个属性value,所以可以直接写。一般需要value="..."

@Component("user")

public class User {

public void sayHello(){

System.out.println("Hello World.");

}

}

配置文件:

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

二、注解进行属性注入

普通属性:@Value(value="..."),这时候可以不写setter方法

对象属性:@Resource(name = "....")

//因为只有一个属性value,所以可以直接写。一般需要value="..."

@Component("user")

public class User {

@Value(value="Spring")

private String s;

public void sayHello(){

System.out.println("Hello World.");

}

@Override

public String toString() {

return "User{" +

"s=‘" + s + ‘\‘‘ +

‘}‘;

}

}

三、XML和注解的混合使用

两种方式结合:一般使用XML注册Bean,使用注解进行属性的注入

首先介绍一些其他的注解配置:

(1)配置 Bean 初始化方法和销毁方法 :* init-method  和 destroy-method.

@PostConstruct  初始化

@PreDestroy 销毁

@PostConstruct

public void setup(){

System.out.println("初始化...");

}

@PreDestroy

public void teardown(){

System.out.println("销毁...");

}

(2) 配置 Bean 的作用范围 :@Scope

@Component("user")

@Scope(value="prototype")

public class User {

@Value(value="Spring")

private String s;

public void sayHello(){

System.out.println("Hello World.");

}

@Override

public String toString() {

return "User{" +

"s=‘" + s + ‘\‘‘ +

‘}‘;

}

}

(3)使用Java类来进行配置信息,在XML中扫描一下即可

@Configuration

public class BeanConfig {

@Bean(name = "car")

public Car showCar() {

Car car = new Car();

car.setName("长安");

car.setPrice(40000d);

return car;

}

@Bean(name = "product")

public Product initProduct() {

Product product = new Product();

product.setName("空调");

product.setPrice(3000d);

return product;

}

}

混合使用范例:

public class Person {

@Autowired

@Qualifier("car")

private Car car;

@Autowired

@Qualifier(value = "plane")

private Plane plane;

@Override

public String toString() {

return "Person{" +

"car=" + car +

", plane=" + plane +

‘}‘;

}

}

配置文件:

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

四、集成Junit测试

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring-config.xml")

public class spring6 {

@Autowired

@Qualifier("person")

private Person p;

@Test

public void demo(){

System.out.println(p);

}

}

配置文件:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值