在Spring4之后 要使用注解開發 必須保證aop包導入了
使用注解需要導入context約束 增加 注解的支持
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--開啟注解的支持-->
<context:annotation-config/>
</beans>
@Component:組件放在類上 說明這個類被Spring管理了 就是bean
import org.springframework.stereotype.Component;
//等價于<bean id="user" class="com.kero.pojo.User"/>
@Component
public class User {
public String name = "xxx";
}
@Value
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//等價于<bean id="user" class="com.kero.pojo.User"/>
@Component
public class User {
@Value("xxx")
//等價于<property name="name" value="xxx"/>
public String name;
}
或者
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//等價于<bean id="user" class="com.kero.pojo.User"/>
@Component
public class User {
public String name;
@Value("xxx")
public void setName(String name) {
this.name = name;
}
}
@Component有幾個衍生的注解 我們在Web開發中會按照MVC三層架構分層
·dao[@Repository]
·service[@Service]
·controller[@Controller]
這四個注解功能一樣 都是代表將某個類注冊到Spring中 裝配Bean
注解的作用域@Scope
@Scope 放在類上,默認是單例模式
@Scope(prototype)是原型模式,每次創建的都是一個新的對象
其作用等價于
補充:
@Scope("singleton") 或者@Scope 單例模式 下面代碼輸出結果為true
@Scope("prototype")下面代碼輸出結果為false
import com.kero.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
User user2 = context.getBean("user", User.class);
System.out.println(user==user2);
}
}
xml vs 注解
·xml更加萬能 適用于任何場合 維護簡單方便
·注解 不是自己類使用不聊 維護相對復雜
最佳實踐:xml用來管理bean
注解只負責完成屬性的注入
我們在使用的過程中 需要注意 使用以下代碼
<!--指定要掃描的包 這個包下的注解就會生效->-->
<context:component-scan base-package="com.kero"/>
<!--開啟注解的支持-->
<context:annotation-config/>
針對最佳實踐的例子
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--指定要掃描的包 這個包下的注解就會生效->-->
<context:component-scan base-package="com.kero"/>
<!--開啟注解的支持-->
<context:annotation-config/>
<bean id="user" class="com.kero.pojo.User" scope="prototype"/>
</beans>
import org.springframework.beans.factory.annotation.Value;
public class User {
@Value("XXX")
public String name;
public void setName(String name) {
this.name = name;
}
}
到此這篇關于Spring詳解使用注解開發流程的文章就介紹到這了,更多相關Spring注解內容請搜索html5模板網以前的文章希望大家以后多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!