JavaWeb_(Spring框架)注解配置
系列博文
JavaWeb_(Spring框架)xml配置文件 传送门
JavaWeb_(Spring框架)注解配置 传送门
Spring注解配置
a)导包和约束:基本包、aop包+context约束;
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 注解开发 --> <!-- 开启组件扫描 base-package 扫描该包下以及子包的所有注解 --> <context:component-scan base-package="com.Gary.bean"/> </beans>
b)将对象注册到容器内;
在User2.java中需要用到<bean name="user" class="com.Gary.bean.User">的地方使用@Component("user")
@Controller() 1对应web层
@Service("user") 对应service
@Repository() 对应dao层
package com.Gary.bean; import org.springframework.stereotype.Component; //<bean name="user" class="com.Gary.bean.User"> @Component("user") public class User2 { private Integer u_id; private String u_username; private String u_password; //加入宠物字段 private Pet u_pet; @Override public String toString() { return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet + "]"; } public Pet getU_pet() { return u_pet; } public void setU_pet(Pet u_pet) { this.u_pet = u_pet; } public User2() { System.out.println("默认使用 User2 对象空参构造方法"); } public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } public String getU_username() { return u_username; } public void setU_username(String u_username) { this.u_username = u_username; } public String getU_password() { return u_password; } public void setU_password(String u_password) { this.u_password = u_password; } }
package com.Gary.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User2; public class Test_Annotation { //Spring是一个容器,它将帮助我们管理对象 @Test public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_annotation.xml"); User2 u2 = (User2) ac.getBean("user"); System.out.println(u2); } }
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 注解开发 --> <!-- 开启组件扫描 base-package 扫描该包下以及子包的所有注解 --> <context:component-scan base-package="com.Gary.bean"/> </beans>
c)用注解配置Scope属性;
在需要加Scope属性的bean实体层下使用
@Service("user") //@Scope默认是单例的 @Scope(scopeName="prototype")
package com.Gary.bean; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; //<bean name="user" class="com.Gary.bean.User"> //@Component("user") @Service("user") //@Scope默认是单例的 @Scope(scopeName="prototype") public class User2 { private Integer u_id; private String u_username; private String u_password; //加入宠物字段 private Pet u_pet; @Override public String toString() { return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet + "]"; } public Pet getU_pet() { return u_pet; } public void setU_pet(Pet u_pet) { this.u_pet = u_pet; } public User2() { System.out.println("默认使用 User2 对象空参构造方法"); } public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } public String getU_username() { return u_username; } public void setU_username(String u_username) { this.u_username = u_username; } public String getU_password() { return u_password; } public void setU_password(String u_password) { this.u_password = u_password; } }
package com.Gary.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Scope; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User2; public class Test_Annotation { //Spring是一个容器,它将帮助我们管理对象 @Test public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_annotation.xml"); User2 u1 = (User2) ac.getBean("user"); User2 u2 = (User2) ac.getBean("user"); //@Scope(scopeName="prototype")多例的 System.out.println(u1==u2); } }
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 注解开发 --> <!-- 开启组件扫描 base-package 扫描该包下以及子包的所有注解 --> <context:component-scan base-package="com.Gary.bean"/> </beans>
d)注解配置init-method与destroy-method;
//在构造方法后调用 @PostConstruct() public void userInit() { System.out.println("构造方法"); } //在销毁方法前调用 @PreDestroy() public void userDestory() { System.out.println("销毁方法"); }
package com.Gary.bean; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; //<bean name="user" class="com.Gary.bean.User"> //@Component("user") @Service("user") public class User2 { private Integer u_id; private String u_username; private String u_password; //加入宠物字段 private Pet u_pet; @Override public String toString() { return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet + "]"; } public Pet getU_pet() { return u_pet; } public void setU_pet(Pet u_pet) { this.u_pet = u_pet; } public User2() { System.out.println("默认使用 User2 对象空参构造方法"); } public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } public String getU_username() { return u_username; } public void setU_username(String u_username) { this.u_username = u_username; } public String getU_password() { return u_password; } public void setU_password(String u_password) { this.u_password = u_password; } //在构造方法后调用 @PostConstruct() public void userInit() { System.out.println("构造方法"); } //在销毁方法前调用 @PreDestroy() public void userDestory() { System.out.println("销毁方法"); } }
package com.Gary.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Scope; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User2; public class Test_Annotation { //Spring是一个容器,它将帮助我们管理对象 @Test public void Test2() { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_annotation.xml"); User2 u1 = (User2) ac.getBean("user"); ac.close(); } }
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 注解开发 --> <!-- 开启组件扫描 base-package 扫描该包下以及子包的所有注解 --> <context:component-scan base-package="com.Gary.bean"/> </beans>
e)注解配置属性注入,值类型与引用类型;
属性、值注入
@Value(value="111111111") private Integer u_id; @Value(value="ggggggary") private String u_username; private String u_password;
package com.Gary.bean; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; //<bean name="user" class="com.Gary.bean.User"> //@Component("user") @Service("user") public class User2 { @Value(value="111111111") private Integer u_id; @Value(value="ggggggary") private String u_username; private String u_password; //加入宠物字段 private Pet u_pet; @Override public String toString() { return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet + "]"; } public Pet getU_pet() { return u_pet; } public void setU_pet(Pet u_pet) { this.u_pet = u_pet; } public User2() { System.out.println("默认使用 User2 对象空参构造方法"); } public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } public String getU_username() { return u_username; } public void setU_username(String u_username) { this.u_username = u_username; } public String getU_password() { return u_password; } public void setU_password(String u_password) { this.u_password = u_password; } //在构造方法后调用 @PostConstruct() public void userInit() { System.out.println("构造方法"); } //在销毁方法前调用 @PreDestroy() public void userDestory() { System.out.println("销毁方法"); } }
package com.Gary.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Scope; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User2; public class Test_Annotation { //Spring是一个容器,它将帮助我们管理对象 @Test public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_annotation.xml"); User2 u1 = (User2) ac.getBean("user"); System.out.println(u1); } }
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 注解开发 --> <!-- 开启组件扫描 base-package 扫描该包下以及子包的所有注解 --> <context:component-scan base-package="com.Gary.bean"/> </beans>
也可以在方法上加注释
@Value("233333333") public void setU_password(String u_password) { this.u_password = u_password; }
Spring能在private私有成员上通过@Value()去赋值,使用了暴力反射去注入的,推荐使用在set()方法上注入属性
引用类型注入
给宠物起名为cat,并给宠物类型和颜色通过@Value赋值
@Component("cat") public class Pet {
@Value("猫猫猫") public void setPetType(String petType) { this.petType = petType; } @Value("灰色") public void setColor(String color) { this.color = color; }
package com.Gary.bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("cat") public class Pet { //宠物类型 猫 狗 private String petType; //宠物颜色 private String color; @Override public String toString() { return "Pet [petType=" + petType + ", color=" + color + "]"; } public String getPetType() { return petType; } @Value("猫猫猫") public void setPetType(String petType) { this.petType = petType; } @Value("灰色") public void setColor(String color) { this.color = color; } public String getColor() { return color; } }
在User.java上使用@Autowired()自动装配
//使用自动装配,加入宠物字段 @Autowired private Pet u_pet;
第一种:用@Autowired()代替,缺点:问题:如果匹配到多个类型一致的对象,将无法注入到具体哪个对象
为了解决这个确定
第二种:用@Qualifier("userService")
第三者@Resource(name="userService")
package com.Gary.bean; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; //<bean name="user" class="com.Gary.bean.User"> //@Component("user") @Service("user") public class User2 { @Value(value="111111111") private Integer u_id; @Value(value="ggggggary") private String u_username; private String u_password; //使用自动装配,加入宠物字段 @Autowired private Pet u_pet; @Override public String toString() { return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet + "]"; } public Pet getU_pet() { return u_pet; } public void setU_pet(Pet u_pet) { this.u_pet = u_pet; } public User2() { System.out.println("默认使用 User2 对象空参构造方法"); } public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } public String getU_username() { return u_username; } public void setU_username(String u_username) { this.u_username = u_username; } public String getU_password() { return u_password; } @Value("233333333") public void setU_password(String u_password) { this.u_password = u_password; } //在构造方法后调用 @PostConstruct() public void userInit() { System.out.println("构造方法"); } //在销毁方法前调用 @PreDestroy() public void userDestory() { System.out.println("销毁方法"); } }
package com.Gary.bean; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; //<bean name="user" class="com.Gary.bean.User"> //@Component("user") @Service("user") public class User2 { @Value(value="111111111") private Integer u_id; @Value(value="ggggggary") private String u_username; private String u_password; //使用自动装配,加入宠物字段 @Autowired private Pet u_pet; @Override public String toString() { return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_pet=" + u_pet + "]"; } public Pet getU_pet() { return u_pet; } public void setU_pet(Pet u_pet) { this.u_pet = u_pet; } public User2() { System.out.println("默认使用 User2 对象空参构造方法"); } public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } public String getU_username() { return u_username; } public void setU_username(String u_username) { this.u_username = u_username; } public String getU_password() { return u_password; } @Value("233333333") public void setU_password(String u_password) { this.u_password = u_password; } //在构造方法后调用 @PostConstruct() public void userInit() { System.out.println("构造方法"); } //在销毁方法前调用 @PreDestroy() public void userDestory() { System.out.println("销毁方法"); } }
package com.Gary.bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("cat") public class Pet { //宠物类型 猫 狗 private String petType; //宠物颜色 private String color; @Override public String toString() { return "Pet [petType=" + petType + ", color=" + color + "]"; } public String getPetType() { return petType; } @Value("猫猫猫") public void setPetType(String petType) { this.petType = petType; } @Value("灰色") public void setColor(String color) { this.color = color; } public String getColor() { return color; } }
package com.Gary.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Scope; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Gary.bean.User2; public class Test_Annotation { //Spring是一个容器,它将帮助我们管理对象 @Test public void Test2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_annotation.xml"); User2 u1 = (User2) ac.getBean("user"); System.out.println(u1); } }
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 注解开发 --> <!-- 开启组件扫描 base-package 扫描该包下以及子包的所有注解 --> <context:component-scan base-package="com.Gary.bean"/> </beans>
(如需转载学习,请标明出处)