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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

spring框架学习笔记3:使用注解代替配置文件

發布時間:2024/1/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring框架学习笔记3:使用注解代替配置文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.導入context約束:spring-context-4.2.xsd

?

2.design模式打開xml配置文件,右鍵edit namespaces,點擊add添加

?

?

完成后應該是這樣:

?

配置文件中這樣寫即可:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "><context:component-scan base-package="bean"></context:component-scan> </beans>

?

在類中這樣使用注解:

package bean;import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource;import org.junit.validator.PublicClassValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service;//代替的配置文件內容<bean name="user" class="bean.User"/> // @Component("user")//四種本質相同,為了方便理解建議使用以下三種 // @Service("user")//service層使用 // @Controller("user")//web層使用@Repository("user")//dao層使用 //指定對象的作用范圍 @Scope(scopeName="singleton") public class User {@Value("Tom")//賦值private String name;private Integer age;//@Autowired//對象賦值,自動裝配//存在問題:如果是多個類型一致的對象,無法分辨@Resource(name="car")//這種方式可以明確指定(推薦)private Car car;public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}@Value("20")//也可以在set方法賦值,效果一樣,但不破壞封裝性public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User [name=" + name + ", age=" + age + ", car=" + car + "]";}@PostConstruct//初始化方法,當相于配置文件中的init-mothodpublic void init(){System.out.println("初始化");}@PreDestroy//銷毀方法public void destory(){System.out.println("銷毀");}}

?

Car類:

package bean;import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component("car") public class Car {@Value("car2")private String name;private String color;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}@Overridepublic String toString() {return "Car [name=" + name + ", color=" + color + "]";}}

?

?

測試可以寫成以前的測試類,如果安裝了STS插件,就可以這樣:

package annotation;import javax.annotation.Resource;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import bean.User;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class Demo {@Resource(name="user")private User u;@Testpublic void fun1(){System.out.println(u);}}

?

轉載于:https://www.cnblogs.com/xuyiqing/p/8463513.html

總結

以上是生活随笔為你收集整理的spring框架学习笔记3:使用注解代替配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。

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