博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring注解开发
阅读量:7086 次
发布时间:2019-06-28

本文共 5300 字,大约阅读时间需要 17 分钟。

IOC注解方式快速入门

1. 在spring4之后,想要使用注解形式,必须得要引入aop的包

1472533-20190510111854179-1699647000.png

2. 在配置文件当中,还得要引入一个context约束

3. 配置组件扫描

哪个包下的类型使用组合扫描

4. 在类开上添加注解

必须在已经扫描包下

package com.xzh.spring.demo1;import org.springframework.stereotype.Controller;@Controller("user")// 相当于配置文件中 
public class User { public String name;}

测试:

@Testpublic void test() {    ApplicationContext applicationContext =            new ClassPathXmlApplicationContext("applicationContext.xml");    User user = (User) applicationContext.getBean("user");    System.out.println(user);}

5. 使用注解注入属性

(1)可以不用提供set方法,直接在直接名上添加@value("值")

package com.xzh.spring.demo1;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;@Controller("user")// 相当于配置文件中 
public class User { @Value("IT666") // 相当于配置文件中
public String name;}

测试:

@Testpublic void test() {    ApplicationContext applicationContext =            new ClassPathXmlApplicationContext("applicationContext.xml");    User user = (User) applicationContext.getBean("user");    System.out.println(user);    System.out.println(user.name);}

(2)如果提供了set方法,在set方法上添加@value("值");

package com.xzh.spring.demo1;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;@Controller("user")public class User {    public String name;    @Value("IT888")    public void setName(String name) {        this.name = name;    }}

测试同(1)

IOC注解详解

@Component

修改一个类,将这个类交给Spring管理。相当于在配置文件当中配置

@Component三个衍生注解

为了更好的进行分层,Spring可以使用其它三个注解,功能类似,目前使用哪一个功能都一样, 后期可能会添加一些属于各自的属性。

@Controller:web层

@Service:service层
@Repository:dao层

@Controller("user")public class User {    @Value("IT666")    public String name;}@Service("user")public class User {    @Value("IT666")    public String name;}@Repository("user")public class User {    @Value("IT666")    public String name;}

属性注入

@Value

设置普通属性值

1472533-20190510111908782-1796398536.png

@Controller("user")public class User {    @Value("IT666")    public String name;}

@Autowired

(1)设置对象类型的属性值,直接使用这种方式,是按照类型完全属性注入,不需要在注解上使用id名称

@Component("dog")public class Dog {    @Value("旺财")    public String name;}
@Controller("user")public class User {    @Value("IT666")    public String name;    @Autowired    public Dog dog;}

测试:

@Test    public void test() {        ApplicationContext applicationContext =                new ClassPathXmlApplicationContext("applicationContext.xml");        User user = (User) applicationContext.getBean("user");        System.out.println(user);        System.out.println(user.name);        System.out.println(user.dog.name);    }

1472533-20190510111924880-2134053319.png

(2)习惯是按照名称完成属性注入,必须让@Autowired注解与@Qualifier一起使用

@Component("dog")public class Dog {    @Value("旺财")    public String name;}
@Controller("user")public class User {    @Value("IT666")    public String name;    @Autowired    @Qualifier("dog")    public Dog dog;}

测试代码同上。

(3)单独使用@Resource,可以按照名称完成属性注入

@Component("dog")public class Dog {    @Value("旺财")    public String name;}
@Controller("user")public class User {    @Value("IT666")    public String name;    @Resource(name = "dog")    public Dog dog;}

测试代码同上。

@PostConstruct 和 @PreDestroy

@PostConstruct 初始化方法,相当于

@PreDestroy 销毁方法,相当于

@Controller("user")public class User {    @Value("IT666")    public String name;    @Resource(name = "dog")    public Dog dog;    @PostConstruct    public void init(){        System.out.println("init----初始化");    }    @PreDestroy    public void destroy(){        System.out.println("destroy----销毁");    }}

测试:

@Testpublic void test() {    ApplicationContext applicationContext =            new ClassPathXmlApplicationContext("applicationContext.xml");    User user = (User) applicationContext.getBean("user");    System.out.println(user);    System.out.println(user.name);    System.out.println(user.dog.name);    ((ClassPathXmlApplicationContext) applicationContext).close();}

@scope

作用范围

  • singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
  • prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收。
@Controller("user")@Scope("prototype")public class User {    @Value("IT666")    public String name;    @Resource(name = "dog")    public Dog dog;    @PostConstruct    public void init(){        System.out.println("init----初始化");    }    @PreDestroy    // 作用范围定为多例,此方法不会调用    public void destroy(){        System.out.println("destroy----销毁");    }}

XML与注解比较

  • XML可以适用任何场景 ,结构清晰,维护方便
  • 注解不是自己提供的类使用不了,开发简单方便

XML与注解整合开发

  • XML管理Bean
  • 注解完成属性注入
  • 使用过程中, 可以不用扫描,扫描是为了类上的注解

在没有扫描的情况下, 使用属性注解@Resource @Value @Autowired @Qulifier

public class Dog {    @Value("旺财")    public String name;}public class User {    @Value("IT666")    public String name;    @Resource(name = "dog")    public Dog dog;    @PostConstruct    public void init(){        System.out.println("init----初始化");    }    @PreDestroy    public void destroy(){        System.out.println("destroy----销毁");    }}

applicationContext.xml 配置:

测试:

@Testpublic void test() {    ApplicationContext applicationContext =            new ClassPathXmlApplicationContext("applicationContext.xml");    User user = (User) applicationContext.getBean("user");    System.out.println(user);    System.out.println(user.name);    System.out.println(user.dog.name);    ((ClassPathXmlApplicationContext) applicationContext).close();}

转载于:https://www.cnblogs.com/xzh0717/p/10843370.html

你可能感兴趣的文章
ansible批量管理远程服务器
查看>>
搭建网页邮件服务器
查看>>
CSS的选择器
查看>>
本人暂时没有文章
查看>>
Oracle用户权限表的管理方法
查看>>
你应该知道的原型图工具Mockplus(摩客)
查看>>
linux
查看>>
从iOS 11看怎样设计APP图标
查看>>
面试:进程调度的任务、机制和方式
查看>>
终于注册好博客了,兴奋
查看>>
我的友情链接
查看>>
mysql批量insert
查看>>
位图图像处理控件ImageCapture Suite更新至v9.1
查看>>
Oracle常用命令
查看>>
我的友情链接
查看>>
spring 工具类简介02
查看>>
I/O基础理论
查看>>
大型网站技术架构的演进
查看>>
使用Apache Tiles3.x构建界面布局(二)
查看>>
iOS APP发布经验(Xcode6.2 2015.3不断更新)
查看>>