Spring Bean裝載方式代碼實(shí)例解析
這篇文章主要介紹了Spring Bean裝載方式代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
Bean的裝配方式
Bean的裝配可以理解為依賴(lài)關(guān)系注入
基于XML的裝配
a) 設(shè)值注入
i.要求:
- Bean 類(lèi)必須提供一個(gè)默認(rèn)的無(wú)參構(gòu)造方法。
- Bean 類(lèi)必須為需要注入的屬性提供對(duì)應(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é)果,重寫(xiě)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實(shí)例 -->
<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實(shí)例 -->
<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>來(lái)為Use r 類(lèi)中對(duì)應(yīng)的list集合屬性注入值。
其中<property>元素用于調(diào)用Bean實(shí)例中的setter方法完成屬性賦值,從而完成依賴(lài)注入。
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 類(lèi)標(biāo)識(shí)為Spring 中的Bean,其寫(xiě)法相當(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 類(lèi)標(biāo)識(shí)為Spring中的Bean,這相當(dāng)于配置文件中<bean id="userService" class="com.itheima.annotation.UserServicelmpl”/> 的編寫(xiě);然后使用@Resource 注解標(biāo)注在屬性u(píng)serDao上,這相當(dāng)于配置文件中<property name="userDao" ref="userDao“/>的寫(xiě)法。
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 類(lèi),這相當(dāng)于在配置文件中編寫(xiě)<bean id="userControll er" class="com .itheima.annotation.UserController"/>; 然后使用了@Resource 注解標(biāo)注在userService 屬性上,這相當(dāng)于在配置文件中編寫(xiě)<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命名空間,在配置文件中開(kāi)啟相應(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實(shí)例
UserController userController = (UserController)applicationContext.getBean("userController");
//調(diào)用UserController中的save()方法
userController.save();
}
}

3.自動(dòng)裝配
<?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屬性完成自動(dòng)裝配 -->
<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)情況下,配置文件中需要通過(guò)ref 來(lái)裝配Bean ,但設(shè)置了autowire=" byName"后,Spring 會(huì)自動(dòng)尋找userServiceBean 中的屬性,并將其屬性名稱(chēng)與配置文件中定義的Bean 做匹配。由于UserServicelmpl 中定義了userDao 屬'性及其setter 方法,這與配置文件中id 為userDao 的Bean 相匹配,所以Spring會(huì)自動(dòng)地將id 為userDao 的Bean 裝配到id 為userService 的Bean 中。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring boot裝載自定義yml文件
- 詳解Java的Spring框架下bean的自動(dòng)裝載方式
- Spring Bean實(shí)例化實(shí)現(xiàn)過(guò)程解析
- Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語(yǔ)言支持操作示例
- Spring實(shí)戰(zhàn)之獲取其他Bean的屬性值操作示例
- Spring實(shí)戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作示例
- Spring如何使用注解的方式創(chuàng)建bean
- Spring實(shí)戰(zhàn)之注入嵌套Bean操作示例
- SpringBoot如何統(tǒng)一配置bean的別名
相關(guān)文章
java開(kāi)發(fā)中常遇到的各種難點(diǎn)以及解決思路方案
Java項(xiàng)目是一個(gè)復(fù)雜的軟件開(kāi)發(fā)過(guò)程,其中會(huì)涉及到很多技術(shù)難點(diǎn),這篇文章主要給大家介紹了關(guān)于java開(kāi)發(fā)中常遇到的各種難點(diǎn)以及解決思路方案的相關(guān)資料,需要的朋友可以參考下2023-07-07
java向文件中追加內(nèi)容與讀寫(xiě)文件內(nèi)容源碼實(shí)例代碼
這篇文章主要介紹了java向文件中追加內(nèi)容與讀寫(xiě)文件內(nèi)容源碼實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
JSR303校驗(yàn)前端傳遞的數(shù)據(jù)方式
這篇文章主要介紹了JSR303校驗(yàn)前端傳遞的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
親手教你SpringBoot中的多數(shù)據(jù)源集成問(wèn)題
本文主要是介紹基于springboot的多數(shù)據(jù)源切換,輕量級(jí)的一種集成方案,對(duì)于小型的應(yīng)用可以采用這種方案,我之前在項(xiàng)目中用到是因?yàn)楹?jiǎn)單,便于擴(kuò)展以及優(yōu)化,對(duì)SpringBoot多數(shù)據(jù)源集成問(wèn)題感興趣的朋友一起看看吧2022-03-03
Spring?Validation實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例
Spring?Validation其實(shí)就是對(duì)Hibernate?Validator進(jìn)一步的封裝,方便在Spring中使用,這篇文章主要介紹了Spring?Validation實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例,需要的朋友可以參考下2023-03-03
mybatis自動(dòng)建表的實(shí)現(xiàn)方法
這篇文章主要介紹了mybatis自動(dòng)建表的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
微服務(wù)?Spring?Boot?整合?Redis?BitMap?實(shí)現(xiàn)?簽到與統(tǒng)計(jì)功能
這篇文章主要介紹了微服務(wù)?Spring?Boot?整合?Redis?BitMap?實(shí)現(xiàn)?簽到與統(tǒng)計(jì)功能,文章簡(jiǎn)單介紹了Redis BitMap 基本用法結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
springboot 排除redis的自動(dòng)配置操作
這篇文章主要介紹了springboot 排除redis的自動(dòng)配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

