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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

spring 注解简单使用

發布時間:2023/12/9 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring 注解简单使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、通用注解

1、項目結構:

2、新建Person類,注解@Component未指明id,則后期使用spring獲取實例對象時使用默認id="person"方式獲取或使用類方式獲取

package hjp.spring.annotation.commen;import org.springframework.stereotype.Component;//@Component @Component("personId") public class Person { private String name; private Integer age; public String getName() {return name; } public void setName(String name) {this.name = name; } public Integer getAge() {return age; } public void setAge(Integer age) {this.age = age; } @Override public String toString() {return "Person [name=" + name + ", age=" + age + "]"; } } Person

3、新建beans.xml文件,相比之前配置多了

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
不再使用bean節點配置,而是使用context:component-scan節點,屬性base-package指明要掃描注解的包
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- <bean id="personId" class="hjp.spring.annotation.commen.Person"></bean> --><context:component-scan base-package="hjp.spring.annotation.commen"></context:component-scan> </beans>

4、新建測試類

package hjp.spring.annotation.commen;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component;public class TestApp {@Testpublic void demo1() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("hjp/spring/annotation/commen/beans.xml");//注解未指定id時,默認id為person//Person person = applicationContext.getBean("person", Person.class); //注解指定id,即@Component("personId")時 //Person person = applicationContext.getBean("personId",Person.class);//不管是否指定id,使用類方式獲取實例都可以Person person = applicationContext.getBean(Person.class);System.out.println(person);} } 測試類

?

二、衍生三層開發注解

@Controller 修飾Web層;@Service 修飾service層;@Repository 修飾dao層

依賴注入:方式1、普通數據 @Value

     ? 方式2、引用數據 @Autowired,默認按照類型進行注入;如果想要按照名稱進行注入,還需要使用注解@Qualifier("名稱")

1、項目結構

2、新建UserDao類

package hjp.spring.annotation.web;import org.springframework.stereotype.Repository;//@Repository @Repository("userDaoId") public class UserDao {public void save() {System.out.println("add user");} } UserDao

3、新建UserService類

package hjp.spring.annotation.web;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;@Service public class UserService {@Autowired//@Qualifier("userDaoId") //指向UserDao類注解@Repository("userDaoId")指定的Id//屬性注入也可以使用注解@Resource//@Resourceprivate UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void addUser() {userDao.save();} } UserService

4、新建UserAction類

package hjp.spring.annotation.web;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller;@Controller("userActionId") //@Scope("prototype")//多例 public class UserAction {@Autowiredprivate UserService userService;public void setUserService(UserService userService) {this.userService = userService;}public void execute() {userService.addUser();} } UserAction

5、新建測試類

package hjp.spring.annotation.web;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestApp {@Testpublic void demo1() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("hjp/spring/annotation/web/beans.xml");UserAction userAction = applicationContext.getBean("userActionId", UserAction.class);System.out.println(userAction);//用于測試單例和多例 userAction.execute();userAction=applicationContext.getBean("userActionId", UserAction.class);System.out.println(userAction);//用于測試單例和多例 } } 測試類

6、新建beans.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/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 id="personId" class="hjp.spring.annotation.commen.Person"></bean> --><!-- 讓spring對含有注解的類進行掃描 --><context:component-scan base-package="hjp.spring.annotation.web"></context:component-scan> </beans> beans.xml

三、其他注解:如@PostConstruct修飾初始化;@PreDestroy修飾銷毀

四、項目中對XML和注解的使用情況

1、純XML,整合第三方(jar包)

2、純注解,限制條件必須有源碼,簡化代碼開發

3、xml+注解,xml配置bean,代碼中使用注入注解

如果混合使用不需要掃描,只需要加入<context:annotation-config></context:annotation-config>配置

測試Demo可以將UserDao類的@Repository、UserService類的@Service、UserAction類的@Controller去掉,只保留注入注解,

然后修改beans.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/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 id="userDaoId" class="hjp.spring.annotation.web.UserDao"></bean><bean id="userServiceId" class="hjp.spring.annotation.web.UserService"></bean><bean id="userActionId" class="hjp.spring.annotation.web.UserAction"></bean><context:annotation-config></context:annotation-config> </beans>

?

總結

以上是生活随笔為你收集整理的spring 注解简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。

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