spring-day2 an'notation包 的类

package annotation;

import java.io.Serializable;

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

/**
 * 演示使用autowired来完成依赖注入 构造器的方式
 * 
 * @author Ward
 *
 */
@Component("hotel")
public class Hotel implements Serializable{
    private Waiter wt;

    public Hotel() {
    }

    @Autowired
    public Hotel(@Qualifier("wt") Waiter wt) {
        System.out.println("Hotel的带参构造器");
        this.wt = wt;
    }

    @Override
    public String toString() {
        return "Hotel [wt=" + wt + "]";
    }
    
    
}

--------------------------------------------------------------------------------------------------------------------------------

package annotation;

import javax.annotation.Resource;

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

/**
 * 演示使用@Resource来完成依赖注入
 * @author Ward
 *
 */
@Component("manager")
public class Manager {    
    private Waiter wt;
    //这个注解也可以添加到对应的set方法前面
    @Value("#{config.pageSize}")
    private String pageSize;
    
    @Value("马云")
    //Value也可以注入基本类型的值
    private  String name;
    public Manager() {
        System.out.println("Manager的无参构造器");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

 


    @Override
    public String toString() {
        return "Manager [wt=" + wt + ", pageSize=" + pageSize + ", name=" + name + "]";
    }

 


    public String getPageSize() {
        return pageSize;
    }

    public void setPageSize(String pageSize) {
        this.pageSize = pageSize;
    }

    public Waiter getWt() {
        return wt;
    }
    
    //name属性指定被注入的bean的id
    //如果不指定,默认ByType的方式来注入。
    @Resource(name="wt")
    public void setWt( Waiter wt) {
        System.out.println("Manager的set方法");
        this.wt = wt;
    }
    
    
}

-------------------------------------------------------------------------------------------------------------------------------

package annotation;

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

/**
 * 演示使用autowired来完成(set)依赖注入
 * 
 *
 */
@Component("rest")
public class Restaurant {

//      @Autowired
//      @Qualifier("wt")    
//这两个注解也可以直接添加到属性前面,效果和加在set方法上是一样的
    //此时对应的set方法可以不要
    private Waiter wt;

    
    public Restaurant() {
    System.out.println("Restaurant的无参构造器");
    }

    public Waiter getWt() {
        return wt;
    }
    
    //如果没有使用Qualifier指定被注入的bean的id,
    //则spring容器默认按ByType的方式来注入
    
    @Autowired
    public void setWt(@Qualifier("wt") Waiter wt) {
        System.out.println("Restaurant的setWt()方法");
        this.wt = wt;
    }

    @Override
    public String toString() {
        return "Restaurant [wt=" + wt + "]";
    }
    
    
}

---------------------------------------------------------------------------------------------------------------------------

package annotation;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 演示组件扫描
 * 默认的id 是类名首字母小写之后的类名 student。
 * getBean("student")
 * 自定义的话加(***),***中写bean的id
 * @Scope("prototype"),某个类型的bean在容器中 默认只有一个实例即单例。加上 prototype:spring创建的某个类型的bean在容器中 
有多个实例,非单例 。加上属性,即可改成创建多个实例 。
@Lazy,设置为true延迟加载
 *
 */

@Component("stu")
@Scope("singleton")
@Lazy(true)
public class Student {
    
    @PostConstruct
    //初始化方法
    public void init() {
        System.out.println("Student的init()...");
    }
    @PreDestroy
    //销毁方法
    public void destroy() {
        System.out.println("Student的destroy...");
    }
    public Student() {
        System.out.println("Student的无参构造器!!");
    }
    
}

------------------------------------------------------------------------------------------------------------------------

package annotation;

import org.springframework.stereotype.Component;

@Component("wt")
public class Waiter {

    public Waiter() {
    System.out.println("Waiter的无参构造器");
    }

}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值