日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

05_IOC容器装配Bean(注解方式)

發布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 05_IOC容器装配Bean(注解方式) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IOC容器裝配Bean(注解方式)?

1.使用注解方式進行Bean注冊?

? ??xml?方式:?<bean?id=""?class="">?

spring2.5版本?提供一組注解,完成Bean注冊?

? ??*?@Component?描述Spring框架中Bean?

導入jar?和?xml方式開發是相同的?

第一步?編寫Class,在聲明上?添加?@Component?

  • /**
  • * 使用Spring2.5注解 注冊Bean
  • */
  • @Component("helloService")
  • // <bean id="helloService" class="...." />
  • publicclassHelloService{
  • publicvoid sayHello(){
  • System.out.println("hello, spring annotation!");
  • }
  • }
  • ?

    第二步?編寫applicationContext.xml?通知Spring注解類所在包?

    *?需要引入?context?名稱空間?

  • <?xml version="1.0" encoding="UTF-8"?>
  • <beansxmlns="http://www.springframework.org/schema/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/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">
  • <!-- 配置 注解Bean 所在包 -->
  • <context:annotation-config/>
  • <context:component-scanbase-package="cn.itcast.spring.a_beandefinition"></context:component-scan>
  • </beans>
  • 進行測試:
  • publicclassSpringTest{
  • @Test
  • // 測試 注解Bean 注冊
  • publicvoid demo1(){
  • ApplicationContext applicationContext =newClassPathXmlApplicationContext("applicationContext.xml");
  • //bean的名稱來自@Component("helloService")
  • HelloService helloService =(HelloService) applicationContext.getBean("helloService");
  • helloService.sayHello();
  • }
  • }
  • spring2.5?引入@Component?等效三個衍生注解?

    *?@Repository?用于對DAO實現類進行標注?(持久層)

    *?@Service?用于對Service實現類進行標注?(業務層)

    *?@Controller?用于對Controller實現類進行標注?(表現層)

    ?

    2.屬性依賴注入?

    1)?簡單屬性的注入?通過?@Value注解完成,不需要提供setter方法

  • @Service("userService")
  • public?class?UserService?{
  • ????//?注入name屬性
  • ????@Value("itcast")
  • ????private?String?name;
  • }
  • ?

    2)?復雜屬性注入,通過@Autowired注解?完成Bean自動裝配?

    @Autowired?默認按照類型進行注入?

  • /**
  • * 用戶操作數據層
  • */
  • @Repository("userDAO")
  • publicclassUserDAO{
  • }
  • /**
  • * 用戶業務層
  • */
  • @Service("userService")
  • publicclassUserService{
  • // 注入name屬性
  • @Value("itcast")
  • // 簡單屬性
  • privateString name;
  • ?
  • //@Autowired默認按照類型
  • @Autowired
  • privateUserDAO userDAO;
  • @Override
  • publicString toString(){
  • return"UserService [name="+ name +", userDAO="+ userDAO +"]";
  • }
  • }
  • @Value?@Autowired注解都可以修飾?成員變量?或者?setter方法,如果修飾成員變量,不需要提供setter方法

    ?

    @Autowired注解?結合?@Qualifer注解按照名稱注入

  • @Service("userService")
  • public?class?UserService?{
  • ????@Autowired
  • ????@Qualifier("uDAO")
  • ????//?復雜對象
  • ????private?UserDAO?userDAO;
  • }
  • @Qualifier("userDAO")注解的名稱必須與@Repository("userDAO")名稱一致,就會報錯!

    ?

  • @Repository("uDAO")
  • public?class?UserDAO?{?
  • }
  • 3)?使用@Resource注解?完成復雜對象Bean裝配?

    @Resource@Autowired注解功能相似

    ?

  • @Autowired
  • @Qualifer("userDAO")
  • private?UserDAO?userDAO?;?
  • 等價于?
  • @Resource(name="userDAO")
  • private?UserDAO?userDAO?;
  • ?

    3.Bean其它屬性設置?

    1)?指定Bean的初始化方法和銷毀方法(注解)??<bean?init-method=""?destroy-method=""?/>

    @PostConstruct??作用?init-method

    @PreDestroy??作用?destroy-method?

  • @Component("lifeBean")
  • publicclassLifeCycleBean{
  • @PostConstruct
  • publicvoid setup(){
  • System.out.println("初始化...");
  • }
  • @PreDestroy
  • publicvoid teardown(){
  • System.out.println("銷毀...");
  • }
  • }
  • 進行測試:
  • @Test
  • // 測試初始化和銷毀
  • publicvoid demo1(){
  • ????ClassPathXmlApplicationContext applicationContext =newClassPathXmlApplicationContext("applicationContext.xml");
  • ????LifeCycleBean lifeCycleBean =(LifeCycleBean) applicationContext.getBean("lifeBean");
  • ????System.out.println(lifeCycleBean);
  • ????// 銷毀方法執行,必須銷毀ApplicationContext
  • ????applicationContext.close();
  • }
  • 2)?Bean的作用范圍??<bean?scope=""?/>?

    @Scope?注解?,默認作用域?singleton?單例?

  • @Component("scopeBean")
  • // 如果沒有指定scope 是 singleton 單例
  • @Scope("prototype")
  • publicclassScopeBean{
  • }
  • 進行測試:
  • @Test
  • // 測試Bean 范圍
  • publicvoid demo2(){
  • ApplicationContext applicationContext =newClassPathXmlApplicationContext("applicationContext.xml");
  • ScopeBean scopeBean =(ScopeBean) applicationContext.getBean("scopeBean");
  • System.out.println(scopeBean);
  • ScopeBean scopeBean2 =(ScopeBean) applicationContext.getBean("scopeBean");
  • System.out.println(scopeBean2);
  • }
  • 4.Spring3.0?提供?注冊Bean的注解

    @Configuration?指定POJO類為Spring提供Bean定義信息

    @Bean?提供一個Bean定義信息

    ?

    先定義2個JavaBean:

  • // 轎車
  • publicclassCar{
  • privateString name;
  • privatedouble price;
  • publicString getName(){
  • return name;
  • }
  • publicvoid setName(String name){
  • this.name = name;
  • }
  • publicdouble getPrice(){
  • return price;
  • }
  • publicvoid setPrice(double price){
  • this.price = price;
  • }
  • @Override
  • publicString toString(){
  • return"Car [name="+ name +", price="+ price +"]";
  • }
  • }
  • // 商品
  • publicclassProduct{
  • privateString pname;
  • privateint pnum;
  • publicString getPname(){
  • return pname;
  • }
  • publicvoid setPname(String pname){
  • this.pname = pname;
  • }
  • publicint getPnum(){
  • return pnum;
  • }
  • publicvoid setPnum(int pnum){
  • this.pnum = pnum;
  • }
  • @Override
  • publicString toString(){
  • return"Product [pname="+ pname +", pnum="+ pnum +"]";
  • }
  • }
  • 此類需要我們自己編寫,好比一個大的工廠。
  • /**
  • * 配置Bean (工廠)
  • */
  • @Configuration
  • publicclassBeanConfig{
  • // 提供兩個方法 獲得Car和Product對象
  • @Bean(name ="car")
  • //方法名稱隨意
  • publicCar initCar(){
  • Car car =newCar();
  • car.setName("大眾");
  • car.setPrice(10000);
  • return car;
  • }
  • @Bean(name ="product")
  • publicProduct showProduct(){
  • Product product =newProduct();
  • product.setPname("空調");
  • product.setPnum(100);
  • return product;
  • }
  • }
  • 進行測試:
  • @Test
  • // 獲得配置Bean 工廠創建Bean對象
  • publicvoid demo(){
  • ApplicationContext applicationContext =newClassPathXmlApplicationContext("applicationContext.xml");
  • Car car =(Car) applicationContext.getBean("car");
  • System.out.println(car);
  • Product product =(Product) applicationContext.getBean("product");
  • System.out.println(product);
  • }
  • 使用配置BeanSpring掃描到,就可以了

    ?

    5.xml和注解混合使用?

    很多企業開發者?還是采用xml作為主流配置?

    *?Bean?注冊?通過XML完成

    *?注入使用?@Autowired?注解完成?

    ?將2個Dao注入到Service

  • // 客戶DAO
  • publicclassCustomerDAO{
  • }
  • // 訂單DAO
  • publicclassOrderDAO{
  • }
  • Service類(注入):
  • // 客戶Service
  • publicclassCustomerService{
  • // xml注入
  • privateCustomerDAO customerDAO;
  • publicvoid setCustomerDAO(CustomerDAO customerDAO){
  • this.customerDAO = customerDAO;
  • }
  • @Override
  • publicString toString(){
  • return"CustomerService [orderDAO="+ orderDAO +", customerDAO="
  • + customerDAO +"]";
  • }
  • }
  • 配置(注冊):
  • <bean id="customerDAO"class="cn.itcast.spring.e_xmluseannotaion.CustomerDAO"></bean>
  • <bean id="orderDAO" class="cn.itcast.spring.e_xmluseannotaion.OrderDAO"></bean>
  • <!--DAO 注入Service-->
  • <bean id="customerService"class="cn.itcast.spring.e_xmluseannotaion.CustomerService">
  • <property name="customerDAO" ref="customerDAO"></property>
  • </bean>
  • 測試:
  • @Test
  • // 完成 DAO 注入到Service測試
  • publicvoid demo(){
  • ApplicationContext applicationContext =newClassPathXmlApplicationContext("applicationContext2.xml");
  • CustomerService customerService =(CustomerService) applicationContext.getBean("customerService");
  • System.out.println(customerService);
  • }
  • *************************************************************************************************************************************** 此時,我們發現,在注冊的時候使用xml比較方便,而在注入的時候使用xml方式比較麻煩,需要提供setter方法,下面使用注解方式注入
  • // 客戶Service
  • publicclassCustomerService{
  • // 注解注入
  • @Autowired
  • privateOrderDAO orderDAO;
  • // xml注入
  • privateCustomerDAO customerDAO;
  • publicvoid setCustomerDAO(CustomerDAO customerDAO){
  • this.customerDAO = customerDAO;
  • }
  • @Override
  • publicString toString(){
  • return"CustomerService [orderDAO="+ orderDAO +", customerDAO="
  • + customerDAO +"]";
  • }
  • }
  • ?

    <context:annotation-config/>?啟用四個注解?使@Resource@?PostConstruct、@?PreDestroy@Autowired注解生效

    結論?:

    ???1、?xml配置?和?注解配置?效果完全相同?

    ???2、?如果Bean?來自第三方(源碼無法改動),?必須使用xml???

    ???3、?Spring3.0?Bean注冊方式,?使用比較少,主要用于Bean?構造邏輯及其復雜



    來自為知筆記(Wiz)



    轉載于:https://www.cnblogs.com/tangwan/p/4674974.html

    總結

    以上是生活随笔為你收集整理的05_IOC容器装配Bean(注解方式)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。