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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring 常用注入注解(annotation)和其对应xml标签

發(fā)布時(shí)間:2023/12/31 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 常用注入注解(annotation)和其对应xml标签 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用注解需要修改bean.xml:

?

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/><bean class="example.SimpleMovieCatalog"><qualifier value="main"/><!-- inject any dependencies required by this bean --></bean><bean class="example.SimpleMovieCatalog"><qualifier value="action"/><!-- inject any dependencies required by this bean --></bean><bean id="movieRecommender" class="example.MovieRecommender"/></beans>

?

黃色部分內(nèi)容會(huì)使spring加載AutowiredAnnotationBeanPostProcessor等可以識(shí)別注解的bean。

?

?

?

?

@AutoWire,自動(dòng)注入,一般放在屬性的set方法上,會(huì)為該屬性自動(dòng)注入。默認(rèn)的注入是使用byType,就是根據(jù)xml中bean的類(lèi)型去匹配。

可以和@Qualifier匹配使用。

?

?

?

注解實(shí)現(xiàn)方式:

package com.bjsxt.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

?

public class UserService {
???
??? private UserDAO userDAO;?
???
??? public void init() {
??????? System.out.println("init");
??? }
???
??? public void add(User user) {
??????? userDAO.save(user);
??? }
??? public UserDAO getUserDAO() {
??????? return userDAO;
??? }
???
??? @Autowired
??? public void setUserDAO(@Qualifier("u") UserDAO userDAO) {//Qualifier:根據(jù)qualifier標(biāo)簽去查找,也可以根據(jù)name或者id去查找匹配的bean
??????? this.userDAO = userDAO;
??? }
???

???
??? public void destroy() {
??????? System.out.println("destroy");
??? }
}

?

?

對(duì)應(yīng)的xml實(shí)現(xiàn)方式:

<?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-2.5.xsd"?? ????????? >

? <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl"> ????? <property name="daoId" value="1"></property>??? ? </bean>??? ? ? <bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">??? ????? <property name="daoId" value="2"></property>??? ? </bean>?? ??? ? <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byType"><!-- 也可以設(shè)在beans標(biāo)簽上,好像是default-autowire scope="prototype"表示每次都創(chuàng)建一個(gè)新的實(shí)例對(duì)應(yīng)的有singleton--只有一個(gè)實(shí)例 -->?? ? </bean>???

</beans>

?

?

?

?

@Resource

此標(biāo)簽也用于注入屬性。相對(duì)來(lái)說(shuō)用的比較多。

該標(biāo)簽需要導(dǎo)入J2EE的JAR包:common-annotations.jar

javax.annotation.Resource,默認(rèn)byName,如果找不到對(duì)應(yīng)name會(huì)去找對(duì)應(yīng)type

?

??? @Resource
??? public void setUserDAO(UserDAO userDAO) {?
??????? this.userDAO = userDAO;
??? }

?

@Resource(name=”uuu”) //可以byName

?

以上兩種注解雖然可以在代碼中設(shè)置注入,但是還要求在xml文件中配置bean。

Component可以省去。

5.10.3 Using filters to customize scanning

By default, classes annotated with @Component, @Repository, @Service, @Controller, or a custom annotation that itself is annotated with @Component are the only detected candidate components. However, you can modify and extend this behavior simply by applying custom filters. Add them as include-filter or exclude-filter sub-elements of thecomponent-scan element. Each filter element requires the type and expression attributes. The following table describes the filtering options.

?

?

<?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-2.5.xsd
?????????? http://www.springframework.org/schema/context
?????????? http://www.springframework.org/schema/context/spring-context-2.5.xsd">
??? <context:annotation-config />
??? <context:component-scan base-package="com.bjsxt"/>

</beans>

?

scan:掃描。表示掃描某個(gè)包和其自包,將能作為組件(添加了@Component)的內(nèi)容解析出來(lái)。

?

package com.bjsxt.dao.impl;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

@Component("u") //這樣寫(xiě)表示這個(gè)類(lèi)是一個(gè)組件,在另外一個(gè)類(lèi)看來(lái)它就是一個(gè)資源。不指定它的key默認(rèn)會(huì)是userDAOImpl(即類(lèi)名首字母改成小寫(xiě))

public class UserDAOImpl implements UserDAO {

??? public void save(User user) {
??????? //Hibernate
??????? //JDBC
??????? //XML
??????? //NetWork
??????? System.out.println("user saved!");
??? }

}

?

package com.bjsxt.service;
import javax.annotation.Resource;

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

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;


@Component("userService")
public class UserService {
???
??? private UserDAO userDAO;?
???
??? public void init() {
??????? System.out.println("init");
??? }
???
??? public void add(User user) {
??????? userDAO.save(user);
??? }
??? public UserDAO getUserDAO() {
??????? return userDAO;
??? }
???
??? @Resource(name="u")
??? public void setUserDAO( UserDAO userDAO) {
??????? this.userDAO = userDAO;
??? }
???

???
??? public void destroy() {
??????? System.out.println("destroy");
??? }
}

?

?

?

?

額外的一些內(nèi)容:

<bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype">

scope對(duì)應(yīng)的注解是@Scope(“”)

init-method對(duì)應(yīng)的注解是@PostConstruct,表示構(gòu)造完成之后再執(zhí)行它注解的方法。

destroy-method對(duì)應(yīng)的注解是@PreDestroy,容器銷(xiāo)毀之前。

總結(jié)

以上是生活随笔為你收集整理的Spring 常用注入注解(annotation)和其对应xml标签的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。