1. 程式人生 > >IOC裝配Bean(註解方式)(5)

IOC裝配Bean(註解方式)(5)

IOC裝配Bean(註解方式)

Spring的註解裝配Bean
Spring2.5 引入使用註解去定義Bean
@Component  描述Spring框架中Bean 
Spring的框架中提供了與@Component註解等效的三個註解:
@Repository 用於對DAO實現類進行標註
@Service 用於對Service實現類進行標註
@Controller 用於對Controller實現類進行標註
***** 三個註解為了後續版本進行增強的.

Bean的屬性註入:

普通屬性;
@Value(value="itcast")
    private String info;

對象屬性:
@Autowired:自動裝配默認使用類型註入.
@Autowired
@Qualifier("userDao")       --- 按名稱進行註入.

@Autowired
@Qualifier("userDao")       
private UserDao userDao;
等價於
@Resource(name="userDao")
private UserDao userDao;

Bean其他的屬性的配置:

配置Bean初始化方法和銷毀方法:
* init-method 和 destroy-method.
@PostConstruct 初始化
@PreDestroy  銷毀

配置Bean的作用範圍:
@Scope

以下是測試案例:

<!-- 掃描多個目錄方法兩種
    第一種:<context:component-scan base-package="cn.spring.demo1,cn.spring.demo2" />
    第二種:<context:component-scan base-package="cn.spring" />
-->
    <context:component-scan base-package="cn.spring" />

UserService.java

package cn.spring.demo1;

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

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.Repository;
import org.springframework.stereotype.Service;

/*
 * 註解的方式裝配bean
 */
//在spring配置文件中<bean id="userservice" class="cn.spring.demo1.UserService">
//@Component("userservice")
@Service("userservice")
//scope默認是單實例 
@Scope(value="prototype")
public class UserService {
    @Value(value="測試1")
    private String info;
    //按類型註入的話@Autowired(required=false 忽略異常)
    //@Autowired(required=true)
    //按名稱註入的話加@Qualifier("userdaoonly") 要跟userdao裏面的@Repository("userdaoonly")一樣(第二種)
    //@Qualifier("userdaoonly")
    //@Resource=@Autowired+@Qualifier("userdaoonly")
    @Resource(name="userdaoonly")
    private UserDao userdao;

    public void sayHello(){
        System.out.println("Hello spring Annotation..."+info+userdao);
    }

/*  @Override
    public String toString() {
        return "UserService [info=" + info + ", userdao=" + userdao + "]";
    }*/

    @PostConstruct
    public void setup(){
        System.out.println("初始化……");
    }

    @PreDestroy
    public void teardwon(){
        System.out.println("銷毀……");
    }
}

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

UserDao.java
package cn.spring.demo1;

import org.springframework.stereotype.Repository;

@Repository("userdaoonly")
public class UserDao {

}
-------------------------------------------------------------------------------------------------------------------------------------
package cn.spring.demo1;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//註解的方式
public class SpringTest1 {
    @Test
    public void demo1() {
        ClassPathXmlApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");

        UserService userservice = (UserService) applicationcontext
                .getBean("userservice");
        //      userservice.sayHello();
        System.out.println(userservice);
        UserService userservice2 = (UserService) applicationcontext
        .getBean("userservice");
        //userservice.sayHello();
        System.out.println(userservice2);
        applicationcontext.close();
    }
}

Spring3.0提供使用Java類定義Bean信息的方法

@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;
    }
}

以下是測試案例:

//這個是java類定義bean信息的方式;
package cn.spring.demo2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

    @Bean(name="car")
    public Car showCar() {
        Car car = new Car();
        car.setName("長安");
        car.setPrice(123.00);
        return car;
    }

    @Bean(name="product")
    public Product showProduct() {
        Product product = new Product();
        product.setName("空調");
        product.setPrice(1234.00);
        return product;
    }
}
-----------------------------------------------------------------------------------------------------------------

package cn.spring.demo2;

public class Car {
    private String name;
    private Double price;

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + ", price=" + price + "]";
    }

}

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

package cn.spring.demo2;

public class Product {
    private String name;
    private Double price;

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product [name=" + name + ", price=" + price + "]";
    }

}

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

package cn.spring.demo2;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.spring.demo1.UserService;

public class SpringTest {
    @Test
    public void demo1() {
        ClassPathXmlApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");

        Car car = (Car) applicationcontext
                .getBean("car");
        Product product = (Product) applicationcontext
                .getBean("product");

        System.out.println(car);
        System.out.println(product);
    }
}

實際開發中使用XML還是註解?

XML:
* bean管理
註解;
* 註入屬性的時候比較方便.

兩種方式結合;一般使用XML註冊Bean,使用註解進行屬性的註入.

<context:annotation-config/>
@Autowired
@Qualifier("orderDao")
private OrderDao orderDao;

最後一個整合一下,一個使用XML註冊bean一個用註釋bean

    <context:annotation-config/>

    <bean id="customerdao" class="cn.spring.demo3.CustomerDao"></bean>

      <bean id="orderdao" class="cn.spring.demo3.OrderDao"></bean>

    <bean id="customerservice" class="cn.spring.demo3.CustomerService">
        <property name="customerdao" ref="customerdao"></property>
        <!--  <property name="orderdao" ref="orderdao"></property>-->
    </bean>
package cn.spring.demo3;

public class CustomerDao {

}
-------------------------------------------
package cn.spring.demo3;

public class OrderDao {

}
--------------------------------------------
package cn.spring.demo3;

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

public class CustomerService {

    private CustomerDao customerdao;

    @Autowired
    @Qualifier("orderdao")
    private OrderDao orderdao;

    public void setCustomerdao(CustomerDao customerdao) {
        this.customerdao = customerdao;
    }

    @Override
    public String toString() {
        return "CustomerService [customerdao=" + customerdao + ", orderdao="
                + orderdao + "]";
    }
}
-----------------------------------------------
測試類寫法
package cn.spring.demo3;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    @Test
    public void demo1() {
        ClassPathXmlApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
                "applicationContext2.xml");

        CustomerService customerservice = (CustomerService) applicationcontext
                .getBean("customerservice");
        System.out.println(customerservice);
    }

}

IOC裝配Bean(註解方式)(5)