欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Bean裝載方式代碼實例解析

 更新時間:2020年02月07日 09:25:57   作者:WODioe  
這篇文章主要介紹了Spring Bean裝載方式代碼實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了Spring Bean裝載方式代碼實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

Bean的裝配方式

Bean的裝配可以理解為依賴關(guān)系注入

基于XML的裝配

  a) 設(shè)值注入

i.要求:

  • Bean 類必須提供一個默認(rèn)的無參構(gòu)造方法。
  • Bean 類必須為需要注入的屬性提供對應(yīng)的setter方法。

  b) 構(gòu)造注入

package com.itheima.assemble;
 
import java.util.List;
 
public class User {
  private String username;
  private Integer password;
  private List<String> List;
  /*
   * 1.使用構(gòu)造注入
   * 1.1提供所有帶參數(shù)的有參構(gòu)造方法
   */
  public User(String username,Integer password,List<String> List){
    super();
    this.username = username;
    this.password = password;
    this.List = List;
  }
  /*
   * 2.使用設(shè)值注入
   * 2.1提供默認(rèn)空構(gòu)造方法
   * 2.2為所有屬性提供setter方法
   */
  public User(){
     
  }
  public void setUsername(String username) {
    this.username = username;
  }
  public void setPassword(Integer password) {
    this.password = password;
  }
  public void setList(List<String> list) {
    List = list;
  }
  @Override
  /*
   * (non-Javadoc)
   * @see java.lang.Object#toString()
   * 為了輸出是看到結(jié)果,重寫toString()方法
   */
  public String toString() {
    return "User [username=" + username + ", password=" + password + ", List=" + List + "]";
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 1.使用構(gòu)造注入方式裝配User實例 -->
<bean id="user1" class="com.itheima.assemble.User">
<constructor-arg index="0" value="tom"></constructor-arg>
<constructor-arg index="1" value="123456"></constructor-arg>
<constructor-arg index="2">
  <list>
  <value>"constructorvalue1"</value>
  <value>"constructorvalue2"</value>
  </list>
</constructor-arg>
</bean>
<!-- 2.使用設(shè)值注入裝配User實例 -->
<bean id="user2" class="com.itheima.assemble.User">
  <property name="username" value="張三"></property>
  <property name="password" value="654321"></property>
  <!-- 注入list集合 -->
  <property name="list">
    <list>
      <value>"setlistvalue1"</value>
      <value>"setlistvalue2"</value>
    </list>
  </property>
</bean>
</beans>

<constructor -arg >元素用于定義構(gòu)造方法的參數(shù),子元素<Iist>來為Use r 類中對應(yīng)的list集合屬性注入值。

其中<property>元素用于調(diào)用Bean實例中的setter方法完成屬性賦值,從而完成依賴注入。

package com.itheima.assemble;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class XmlBeanAssembleTest {
  public static void main(String[] args) {
    //定義配置文件路徑
    String xmlPath = "com/itheima/assemble/beans5.xml";
    //加載配置文件
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    //構(gòu)造方式輸出結(jié)果
    System.out.println("構(gòu)造方式:");
    System.out.println(applicationContext.getBean("user1"));
    //設(shè)值方式輸出結(jié)果
    System.out.println("設(shè)值方式:");
    System.out.println(applicationContext.getBean("user2"));
  }
}

2.基于Annotation的裝配

package com.itheima.annotation;
 
public interface UserDao {
  public void save();
}
package com.itheima.annotation;
 
import org.springframework.stereotype.Repository;
 
@Repository("userDao")
public class UserDaoImpl implements UserDao{
  public void save(){
    System.out.println("userdao...save...");
  }
}

先使用@Repository 注解將UserDaolmpl 類標(biāo)識為Spring 中的Bean,其寫法相當(dāng)于配置文件中<bean id="userDao" class="com.itheima.annotation.UserDaolmpl"/>

package com.itheima.annotation;
 
public interface UserService {
  public void save();
}
package com.itheima.annotation;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
@Service("userService")
public class UserServiceImpl implements UserService{
  @Resource(name="userDao")
  private UserDao userDao;
  @Override
  public void save() {
    // TODO Auto-generated method stub
    //調(diào)用userDao中的save()方法
    this.userDao.save();
    System.out.println("userservice...save...");
  }
  public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
  }
   
}

@Service 注解將UserServicelmpl 類標(biāo)識為Spring中的Bean,這相當(dāng)于配置文件中<bean id="userService" class="com.itheima.annotation.UserServicelmpl”/> 的編寫;然后使用@Resource 注解標(biāo)注在屬性userDao上,這相當(dāng)于配置文件中<property name="userDao" ref="userDao“/>的寫法。

package com.itheima.annotation;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
 
@Controller("userController")
public class UserController {
  @Resource(name="userService")
  private UserService userService;
  public void save(){
    this.userService.save();
    System.out.println("userControlle...save...");
  }
  public void setUserService(UserService userService) {
    this.userService = userService;
  }
   
}

Controller 注解標(biāo)注了UserController 類,這相當(dāng)于在配置文件中編寫<bean id="userControll er" class="com .itheima.annotation.UserController"/>; 然后使用了@Resource 注解標(biāo)注在userService 屬性上,這相當(dāng)于在配置文件中編寫<propertyname="userService" ref="userService" />

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="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-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 使用context命名空間,在配置文件中開啟相應(yīng)的注釋處理器 -->
<context:component-scan base-package="com.itheima.annotation"></context:component-scan>
 
</beans>
package com.itheima.annotation;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class AnnotationAssembleTest {
  public static void main(String[] args) {
    String xmlPath = "com/itheima/annotation/beans6.xml";
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    //獲取UserController實例
    UserController userController = (UserController)applicationContext.getBean("userController");
    //調(diào)用UserController中的save()方法
    userController.save();
  }
}

3.自動裝配

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="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-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 使用bean元素的autowire屬性完成自動裝配 -->
<bean id="userDao" class="com.itheima.annotation.UserDaoImpl"></bean>
<bean id="userService" class="com.itheima.annotation.UserServiceImpl" autowire="byName"></bean>
<bean id="userController" class="com.itheima.annotation.UserController" autowire="byName"></bean>
</beans>

增加了autowire 屬性,并將其屬性值設(shè)置為byName 。在默認(rèn)情況下,配置文件中需要通過ref 來裝配Bean ,但設(shè)置了autowire=" byName"后,Spring 會自動尋找userServiceBean 中的屬性,并將其屬性名稱與配置文件中定義的Bean 做匹配。由于UserServicelmpl 中定義了userDao 屬'性及其setter 方法,這與配置文件中id 為userDao 的Bean 相匹配,所以Spring會自動地將id 為userDao 的Bean 裝配到id 為userService 的Bean 中。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能

    Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能

    這篇文章主要介紹了Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能,本文是小編整理的代碼筆記,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-05-05
  • java數(shù)組復(fù)制的四種方法效率對比

    java數(shù)組復(fù)制的四種方法效率對比

    這篇文章主要介紹了java數(shù)組復(fù)制的四種方法效率對比,文中有簡單的代碼示例,以及效率的比較結(jié)果,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • pom文件中${project.basedir}的使用

    pom文件中${project.basedir}的使用

    這篇文章主要介紹了pom文件中${project.basedir}的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java實現(xiàn)迅雷地址轉(zhuǎn)成普通地址實例代碼

    Java實現(xiàn)迅雷地址轉(zhuǎn)成普通地址實例代碼

    本篇文章主要介紹了Java實現(xiàn)迅雷地址轉(zhuǎn)成普通地址實例代碼,非常具有實用價值,有興趣的可以了解一下。
    2017-03-03
  • SpringBoot指標(biāo)監(jiān)控的實現(xiàn)

    SpringBoot指標(biāo)監(jiān)控的實現(xiàn)

    本文主要介紹了SpringBoot指標(biāo)監(jiān)控的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • MyBatis關(guān)閉一級緩存的兩種方式(分注解和xml兩種方式)

    MyBatis關(guān)閉一級緩存的兩種方式(分注解和xml兩種方式)

    這篇文章主要介紹了MyBatis關(guān)閉一級緩存的兩種方式(分注解和xml兩種方式),mybatis默認(rèn)開啟一級緩存,執(zhí)行2次相同sql,但是第一次查詢sql結(jié)果會加工處理這個時候需要關(guān)閉一級緩存,本文給大家詳細(xì)講解需要的朋友可以參考下
    2022-11-11
  • Java實現(xiàn)去掉字符串重復(fù)字母的方法示例

    Java實現(xiàn)去掉字符串重復(fù)字母的方法示例

    這篇文章主要介紹了Java實現(xiàn)去掉字符串重復(fù)字母的方法,涉及java針對字符串的遍歷、判斷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • Spring MVC 中 短信驗證碼功能的實現(xiàn)方法

    Spring MVC 中 短信驗證碼功能的實現(xiàn)方法

    短信驗證功能在各個網(wǎng)站應(yīng)用都非常廣泛,那么在springmvc中如何實現(xiàn)短信驗證碼功能呢?今天小編抽時間給大家介紹下Spring MVC 中 短信驗證碼功能的實現(xiàn)方法,一起看看吧
    2016-09-09
  • Spring中@Service注解的作用與@Controller和@RestController之間區(qū)別

    Spring中@Service注解的作用與@Controller和@RestController之間區(qū)別

    這篇文章主要介紹了Spring中@Service注解的作用與@Controller和@RestController之間的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-03-03
  • Java線程池配置的一些常見誤區(qū)總結(jié)

    Java線程池配置的一些常見誤區(qū)總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java線程池配置的一些常見誤區(qū),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評論