系列博文

    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框架)注解配置的更多相关文章

  1. JavaWeb_(Spring框架)xml配置文件

    系列博文 JavaWeb_(Spring框架)xml配置文件  传送门 JavaWeb_(Spring框架)注解配置 传送门 Xml配置 a)Bean元素:交由Spring管理的对象都要配置在bean ...

  2. JavaWeb_(Spring框架)Spring整合Hibernate

    Dao层类要继承HibernateDaoSupport.java父类 原先使用Hibernate框架hibernate.cfg.xml配置数据库 <hibernate-configuration ...

  3. 跟着刚哥学习Spring框架--事务配置(七)

    事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...

  4. java框架之Spring(2)-注解配置IOC&AOP配置

    注解配置IoC 准备 1.要使用注解方式配置 IoC,除了之前引入的基础 jar 包,还需要引入 spring-aop 支持包,如下: 2.在 applicationContext.xml 中引入 c ...

  5. Spring框架学习 - 配置

    [资料] ★★☆ Spring 中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.Servl ...

  6. 简单实现Spring框架--注解版

    自己写的Spring框架——简单实现IoC容器功能 前几天在网上看了篇帖子,是用xml的方式实现spring的ioc容器,觉得挺有意思的,这边自己试着用注解的形式造了一套轮子. 工程结构 codein ...

  7. JavaWeb_(Spring框架)Spring中的aop事务

    1.事务相关知识 a)什么是事务:把多条数据库操作捆绑到一起执行,要么都成功,要么都失败: b)事务的原则ACID: i.原子性:事务包含的所有操作,要么全部成功,要么全部失败回滚,成功全部应用到数据 ...

  8. Spring纯注解配置

    待改造的问题 我们发现,之所以我们现在离不开 xml 配置文件,是因为我们有一句很关键的配置: <!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器 ...

  9. spring aop注解配置

    spring aop是面向切面编程,使用了动态代理的技术,这样可以使业务逻辑的代码不掺入其他乱七八糟的代码 可以在切面上实现合法性校验.权限检验.日志记录... spring aop 用的多的有两种配 ...

随机推荐

  1. (二)SpringBoot之springboot开发工具的使用以及springboot插件的功能

    一.springboot开发工具的使用 1.1 在项目中添加springoot开发工具 1.2 功能 修改代码后点击保存自动重启 二.springboot插件的功能 2.1 maven配置 <p ...

  2. mysql 添加省市编码表

    省表格: --省级 Provincial create table Provincial(pid int,Provincial varchar(50),primary key (pid)) inser ...

  3. oracle学习笔记:字符串替换 replace、regexp_replace、translate函数

    1.replace 函数 语法:replace(char, search_string, replacement_string) --针对字符串替换 功能: ​ 将char中的字符串替换. ​ 当re ...

  4. Shell学习笔记:awk实现group by分组统计功能

    日常部分数据以 txt 的文件格式提供,为避免入库之后再进行统计的麻烦,故学习 shell 进行处理,减少工作量. 1.样例数据 # test.txt YD5Gxxx|6618151|68254490 ...

  5. Servlet实现图片文件上传

    1.首先要导入以下两个jar包: commons-fileupload-1.2.1.jarcommons-io-1.4.jar 2.jsp文件:index.jsp <%@ page langua ...

  6. 《数据结构与算法之美》 <03>数组:为什么很多编程语言中数组都从0开始编号?

    提到数组,我想你肯定不陌生,甚至还会自信地说,它很简单啊. 是的,在每一种编程语言中,基本都会有数组这种数据类型.不过,它不仅仅是一种编程语言中的数据类型,还是一种最基础的数据结构.尽管数组看起来非常 ...

  7. 向量的一种特殊乘法 element wise multiplication

    向量的一种特殊乘法 element wise multiplication 物体反射颜色的计算采用这样的模型: vec3 reflectionColor = objColor * lightColor ...

  8. Ubuntu系统---编译opencv程序的几种方式g++、Makefile、Cmake

    Ubuntu系统---编译opencv程序的几种方式g++.Makefile.Cmake 先建立一个工程(一个文件夹),写好xxx.cpp文件,可以是多个: //----------opencv.cp ...

  9. JS export 异步导出

    function getUrl () { req().then(res => { console.log(res); }).catch(err => { console.log(err); ...

  10. Java并发包--线程池框架

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3509903.html 线程池架构图 线程池的架构图如下: 1. Executor 它是"执行者 ...