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>
applicationContext_annotation.xml
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;
} }
User2.java
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); } }
Test_Annotation.java
<?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>
applicationContext_annotation.xml
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;
} }
User2.java
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); } }
Test_Annotation.java
<?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>
applicationContent_annotation.xml
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("销毁方法");
} }
User2.java
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(); } }
Test_Annotation.java
<?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>
applicationContext_annotation.xml
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("销毁方法");
} }
User2.java
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); } }
Test_Annotation.java
<?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>
applicationContext_annotation.xml
也可以在方法上加注释
@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;
} }
Pet.java
在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("销毁方法");
} }
User2.java

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("销毁方法");
} }
User2.java
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;
} }
Pet.java
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); } }
Test_Annotation.java
<?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>
applicationContext_annotation.xml
JavaWeb_(Spring框架)注解配置的更多相关文章
- JavaWeb_(Spring框架)xml配置文件
系列博文 JavaWeb_(Spring框架)xml配置文件 传送门 JavaWeb_(Spring框架)注解配置 传送门 Xml配置 a)Bean元素:交由Spring管理的对象都要配置在bean ...
- JavaWeb_(Spring框架)Spring整合Hibernate
Dao层类要继承HibernateDaoSupport.java父类 原先使用Hibernate框架hibernate.cfg.xml配置数据库 <hibernate-configuration ...
- 跟着刚哥学习Spring框架--事务配置(七)
事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...
- java框架之Spring(2)-注解配置IOC&AOP配置
注解配置IoC 准备 1.要使用注解方式配置 IoC,除了之前引入的基础 jar 包,还需要引入 spring-aop 支持包,如下: 2.在 applicationContext.xml 中引入 c ...
- Spring框架学习 - 配置
[资料] ★★☆ Spring 中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.Servl ...
- 简单实现Spring框架--注解版
自己写的Spring框架——简单实现IoC容器功能 前几天在网上看了篇帖子,是用xml的方式实现spring的ioc容器,觉得挺有意思的,这边自己试着用注解的形式造了一套轮子. 工程结构 codein ...
- JavaWeb_(Spring框架)Spring中的aop事务
1.事务相关知识 a)什么是事务:把多条数据库操作捆绑到一起执行,要么都成功,要么都失败: b)事务的原则ACID: i.原子性:事务包含的所有操作,要么全部成功,要么全部失败回滚,成功全部应用到数据 ...
- Spring纯注解配置
待改造的问题 我们发现,之所以我们现在离不开 xml 配置文件,是因为我们有一句很关键的配置: <!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器 ...
- spring aop注解配置
spring aop是面向切面编程,使用了动态代理的技术,这样可以使业务逻辑的代码不掺入其他乱七八糟的代码 可以在切面上实现合法性校验.权限检验.日志记录... spring aop 用的多的有两种配 ...
随机推荐
- 请问IOS中做一个手机网站的app壳复杂吗?
公司开发了一个平台,手机网站已经做出来了,想开发一个苹果应用app,但公司没人会IOS开发,为了减小成本,现在想直接做一个壳来加载手机网站,请问在ios中复杂吗?是否有相应的控件直接加载url就行? ...
- 在.netcore webapi项目中使用后台任务工具Hangfire
安装Hangfire 在webapi项目中通过nuget安装Hangfire.Core,Hangfire.SqlServer,Hangfire.AspNetCore,截止到目前的最新版本是1.7.6. ...
- restTemplate源码解析(二)restTemplate的核心逻辑
所有文章 https://www.cnblogs.com/lay2017/p/11740855.html 正文 上一篇文章中,我们构造了一个RestTemplate的Bean实例对象.本文将主要了解一 ...
- 自定义centos
目录 自定义centos 1. 为什么要自定义centos 2. 自定义centos步骤 自定义centos 1. 为什么要自定义centos 在使用官网的 centos镜像,只有200m,很小,但是 ...
- KVM之virsh管理虚拟机CPU
查看虚拟机CPU数量配置 [root@ubuntu ~]# virsh vcpucount centos_server01 maximum config 2 maximum live 2 curren ...
- XML文件解析之DOM解析
XML文件是一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.基本的解析方式包括DOM解析和SAX解析,具体来说包括DOM解析,SAX解析,DOM4J解 ...
- linux内核驱动学习指南
1. 参考链接 小白的博客 ONE_Tech 你为什么看不懂Linux内核驱动源码? 求教怎么学习linux内核驱动
- php curl请求接口并获取数据
当我们在做php开发的时候,很多时候需要对接口进行测试,或者更方便的调用一些已有模块的接口,取到结果并进行后续操作,我们可以通过curl进行模拟提交post和get请求,来去实现这些功能. 下面是对c ...
- powerlink的Windows-DEMO生成笔记
资料准备: 1.Visual studio 2010 2.Cmake 3.Powerlink 2.7.1源码 具体下载请到相关页面去获取. 新版的powerlink分为两个部分: 1.协议栈 2.应用 ...
- mysql 创建用户并授权数据库
create user test identified by ‘password’:password 你要创建的用户对应的密码 grant all on database.* to test; ...