码迷,mamicode.com
首页 > 编程语言 > 详细

spring系列3 基于注解的IOC

时间:2020-03-24 23:12:35      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:factory   import   pos   package   必须   oid   inter   struct   lap   

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("销毁方法执行了");
    }

spring系列3 基于注解的IOC

标签:factory   import   pos   package   必须   oid   inter   struct   lap   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!