spring系列3 基于注解的IOC

spring中ioc的常用注解

 <bean id="accountService" class="com.mantishell.service.impl.AccountServiceImpl"
       scope="" init-method="" destroy-method="">
     <property name="" value="" | ref=""/>
 </bean>

Componment

用于创建对象,作用和标签相同

  • Component:

    作用:用于把当前类对象存入spring容器中

    属性:value-用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母小写

  • Controller:一般用在表现层
  • Service:一般用在业务层
  • Repository:一般用在持久层

    以上三个注解的作用、属性和Componment相同

    他们三个是spring框架为我们提供明确的三层使用的注解,使三层对象更加清晰

注意使用注解,需要把配置文件中头部信息更换为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
    context名称空间和约束中-->
    <context:component-scan base-package="com.mantishell"/>
</beans>

持久层

IAccountDao.java

package com.mantishell.dao;
public interface IAccountDao {
    void saveAccount();
}

AccountDaoImpl.java

package com.mantishell.dao.impl;
import com.mantishell.dao.IAccountDao;
import org.springframework.stereotype.Repository;

@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    public void saveAccount() {
        System.out.println("保存了账户");
    }
}

业务层

IAccountService.java

package com.mantishell.service;
public interface IAccountService {
    void saveAccount();
}

AccountServiceImpl.java

package com.mantishell.service.impl;
import com.mantishell.dao.IAccountDao;
import com.mantishell.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao = null;
    public void saveAccount() {
        accountDao.saveAccount();
    }
}

表现层

package com.mantishell.ui;
import com.mantishell.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) ac.getBean("accountService");
        as.saveAccount();
    }
}

Autowired

用于注入数据

作用和xml配置文件中的bean标签中写的作用相同

  • Autowired:

    • 作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功

      • 如果ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错。
      • 如果Ioc容器中有多个类型匹配时:
    • 出现位置: 可以是变量上,也可以是方法上
    • 细节: 在使用注解注入时,set方法就不是必须的了。
  • Qualifier:
    • 作用:在按照类中注入的基础之上再按照名称注入。它在给类成员注入时必须和Autowired一起使用。但是在给方法参数注入时可以单独使用。
    • 属性: value:用于指定注入bean的id。
  • Resource
    • 作用:直接按照bean的id注入。它可以独立使用
    • 属性: name:用于指定bean的id。

      以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。

      另外,集合类型的注入只能通过XML来实现。

  • Value
    • 作用:用于注入基本类型和String类型的数据
    • 属性: value:用于指定数据的值。它可以使用spring中SpEL(也就是spring的el表达式)

      SpEL的写法:${表达式}

    @Autowired
    @Qualifier("accountDao1")//指定使用id=accountDao1的bean对象
    private IAccountDao accountDao = null;

或者

    @Resource(name="accountDao2")//指定使用id=accountDao2的bean对象
    private IAccountDao accountDao = null;

Scope

改变作用范围

作用和bean标签中scope属性功能相同,常用取值:singleton prototype

@Scope("singleton")
public class AccountServiceImpl implements IAccountService {

PreDestry和PostConstruct

作用和bean标签中的init-method和destroy-methode一样

PreDestroy:用于指定销毁方法

PostConstruct:用于指定初始化方法

    @PostConstruct
    public void init(){
        System.out.println("初始化方法执行了");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("销毁方法执行了");
    }

原文地址:https://www.cnblogs.com/mantishell/p/12557867.html

时间: 2024-08-29 10:16:49

spring系列3 基于注解的IOC的相关文章

spring学习2:基于注解+xml实现ioc和依赖注入

spring学习2:基于注解+xml实现ioc和依赖注入 一.在spring配置文件中开启spring对注解的支持 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&qu

spring基于注解的IOC(2)

spring第二天:spring基于注解的IOC以及IoC的案例1.spring中ioc的常用注解 用于创建对象的:Component.Controller.Service.Repository 用于注入数据的:Autowired.Qualifier.Resource.Value 用于改变作用范围的:Scope . 和生命周期相关:PreDestroy .PostConstruct 2.案例使用xml方式和注解方式实现单表的CRUD操作 持久层技术选择:dbutils3.改造基于注解的ioc案例

使用 Spring 2.5 基于注解驱动的 Spring MVC--转

概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 XML 配置文件中定义请求和 Controller 的映射关系,仅仅使用注解就可以让一个 POJO 具有 Controller 的绝大部分功能 —— Spring MVC 框架的易用性得到了进一步的增强.在框架灵活性.易用性和扩展性上,Spring MVC 已经全面超越了其它的 MVC 框架,伴随

使用 Spring 2.5 基于注解驱动的 Spring MVC

使用 Spring 2.5 基于注解驱动的 Spring MVC 原文链接:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 基于注解的配置有越来越流行的趋势,Spring 2.5 顺应这种趋势,为 Spring MVC 提供了完全基于注解的配置.本文将介绍 Spring 2.5 新增的 Sping MVC 注解功能,讲述如何使用注解配置替换传统的基于 XML 的 Spring MVC 配置. 概述 继 Spring 2.0

基于注解的Ioc配置 —— Spring(五)

注解配置和Xml配置要实现的功能是一样的,都是要降低程序间的耦合,只是配置的形式不一样. 实际开发中,具体采用注解还是Xml配置,按公司要求. 常用注解: @Component:把资源让Spring来管理,相当于在xml当中配置一个bean. value:指定bean的id,如果不指定value属性,默认bean的id是当前类的类名.首字母小写. @Controller @Service @Repository:都是@Component的衍生注解,作用以及属性都是一样的,只不过提供了更加明确的语

spring AOP (包含基于注解和配置文件两种方式)

spring AOP?面向切面编程,区别于面向对象编程OOP AspectJ: 是Java社区里面最完整最流行的AOP框架,下面就用aspectJ来上例子 一.基于注解方式 步骤如下: 引入jar包(spring的必要jar包 以及aspectj的jar包) 业务方法HelloworldService (类上加上注解@Component,放入到spring ioc容器中) 切面LogingAop (类上加上注解@Component使其加入到ioc容器中,还需要注解@Aspect,使其成为一个切面

Spring MVC中基于注解的 Controller

终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响应请求.实际上,ControllerClassNameHandlerMapping, MultiActionController 和选择恰当的 methodNameResolver(如 InternalPathMethodNameResolver) 就已经可以在很大程度上帮助我们省去不少的 XML 配置,谁让

Spring(七)之基于注解配置

基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. 在 XML 注入之前进行注解注入,因此后者的配置将通过两种方式的属性连线被前者重写. 一.@Required注解 @Required 注解应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitiali

spring的基于注解的IOC配置

1.配置文件配置 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework