您现在的位置是:首页 > 个人日记个人日记

spring-06-使用注解注入

2019-02-03 18:42:21【个人日记】391人已围观

简介使用注解方式进行Bean管理,非常方便

使用注解注入

三种标签

首先配置spring-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--扫描这些类的注解-->
  10. <context:component-scan base-package="com.huangxin.spring"></context:component-scan>
  11. </beans>

当简单数据类型则使用@Component,因为没有使用setter方法,使用反射,所以可以删除setter方法,@Component(这里面可以起名称,等同于xml配置中的id属性),当赋值时使用@Value(填写值)

  1. package com.huangxin.spring;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. @Component("id")
  5. //取代之前Bean标签里的class属性
  6. //如果不使用括号里自定义的,则默认名称为方法名
  7. public class Message {
  8. //通过反射
  9. @Value("hello java")
  10. private String message;
  11. private String name;
  12. public Message(String message, String name) {
  13. this.message = message;
  14. this.name = name;
  15. }
  16. Message() {
  17. System.out.println("初始化完成");
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public void init() {
  26. System.out.println("init...");
  27. }
  28. public void destroy() {
  29. System.out.println("destroy");
  30. }
  31. public String getMessage() {
  32. return message;
  33. }
  34. public void setMessage(String message) {
  35. this.message = message;
  36. }
  37. }

当使用service层

  1. package com.huangxin.spring.service.Impl;
  2. import com.huangxin.spring.service.MessageService;
  3. import org.springframework.stereotype.Service;
  4. @Service("impl02")
  5. public class MessageServiceImpl02 implements MessageService {
  6. public void deleteById(Long id) {
  7. System.out.println("Impl02=已删除id=" + id + "的信息");
  8. }
  9. }

这里实例化直接使用@Service(方法等同于@Component()使用方法)

当使用复杂对象注入时使用@Controller(名称)
赋值使用@Autowired但仅限于有一个实现类,如有多个则使用 @Qualifier(填写需要使用的id),当有多个实现类时也可以使用@Resource(name="这里使用id")

  1. package com.huangxin.spring.controller;
  2. import com.huangxin.spring.service.MessageService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.stereotype.Controller;
  6. @Controller("controller")
  7. public class MessageController {
  8. // @Autowired()//接口唯一实现类时可以直接使用
  9. //如果要使用则实例化实现类
  10. @Autowired
  11. @Qualifier("impl01")//若要使用不唯一的实现类,则需要加入Qualifier注解,需填写Bean的名称,也可以使用Resource()直接使用名称
  12. private MessageService messageService;
  13. public void doGet(Long id) {
  14. messageService.deleteById(id);
  15. }
  16. }

注意

1) 以上三个注解用法相同,都是为类创建Bean
2) 当使用不同注入时应使用不同的注解

两种生命周期
* @PostConstruct//初始化方法,对象创建时调用

  • @PreDestroy//当对象被摧毁时调用的方法

作用域

  • @scope(单例/多例)

Tags: JavaWeb  

评论区

    2024-04-27 08:02:50

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我