spring基于注解的IOC环境搭建配置以及案例

1.环境搭建,在bean.xml导入约束,可在官网获取,pom.xml导入坐标

<?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">

    <context:annotation-config/>

</beans>

2.配置xml

<?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="sise.cn"></context:component-scan>
</beans>

用于创建对象的注解:把对象存入spring容器中


3.service,serviceImpl

/**
 * 账户业务层的接口
 */
public interface IAccountService {

    /**
     * 模拟保存账户
     */
    void saveAccount();
}






/**
 * 账户的业务层实现类
 *
 * xml的配置
 * <bean id="accountService" class="sise.cn.service.Impl.AccountServiceImpl" scope="" init-method="" ref="">
 *     <propertry name="" value=""></propertry>
 *     </bean>
 *
 * 用于创建对象的
 *         他们的的作用就和在xml配置文件中编写一个<bean></bean>标签实现的功能是一样的
 *         @Component:
 *              作用:把当前类对象存入spring容器中
     *              属性:value:用于指定bean的id,当不写时默认值是当前类名且首字母改小写
  * 	   Controller:一般用在表现层
 *         Service:一般用在业务层
 *         Repository:一般用在持久层
 
@Component("accountService")
public class AccountServiceImpl implements IAccountService {

    public void  saveAccount(){
        System.out.println("--------------");
    }
}
/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {

    /**
     * 获取spring的Ioc核心容器,并根据id获取对象
     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//读取配置文件
        //2.根据id即唯一标志获取Bean对象
        IAccountService as = ac.getBean(""accountService"", IAccountService.class);
        System.out.println(as);
        //as.saveAccount();
    }
}

用于注入数据的注解:自动按照类型注入


用于注入数据的注解实现方法原理
在这里插入图片描述
1.service serviceImpl

 /**
     * 账户业务层的接口
     */
    public interface IAccountService {
    
   
/**
 * 账户的业务层实现类
 * 用于注入数据的
 *         他们的作用就何在xml配置文件中的bean标签中写一个<property></property>标签的作用是一样的
 *         Autowired:
 *              作用:自动按照类型注入。只要容器中有唯一的bean对象类型和要注入的变量类型匹配,就可以注入成功
 *                      如果ioc容器类型中没有任何bean类型和要注入的变量类型匹配则报错。
 *                      如果ioc容器中有多个类型匹配时:先匹配
 *              出现位置:
 *                  可以是变量上。也可以是方法上
 *              细节:
 *                 在使用注解注入时,set方法就不是必须的了
 *              Qualifier
 *                  作用:在按照类中注入的基础之上再按照名称注入,它在给类成员注入时不能单独使用,但是在给方法注入时可以
 *                  value:用于指定注入的bean的id
 *              Resource
 *                  作用:直接按照bean的id注入,可以独立使用
 *                  属性:name:用于指定bean的id
 *
 *           Value:
 *              作用:用于注入基本类型和String类型的数据
 *              属性:value用于指定数据的值,他可以使用spring中的SpEl(就是spring的el表达式)
 *                  SpEl的写法: ${表达式}
 * 用于改变作用范围的
 *         他们的作用就和在bean标签中使用scope属性实现的功能是一样的
 * 和生命周期相关的
 *          他们的作用就和在bean标签中使用init-method和destory-method的作用是一样的
 */


@Component("accountService")
public class AccountServiceImpl implements IAccountService {
    //@Autowired//会自动按照类型注入,会到spring容器中找value中的AccountDaoImpl
    //@Qualifier("accountDao1")//需要和@Autowired一起用*/

    @Resource(name="accountDao1")
    private IAccountDao accountDao;

    public void  saveAccount(){
        accountDao.saveAccount();
    }
}

2.dao.daoImpl

public interface IAccountDao {
    void saveAccount();
}


@Repository("accountDao1")
public class AccountDaoImpl implements IAccountDao {

    public void saveAccount() {
        System.out.println("保存了账户");
    }
}

test:

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {

    /**
     * 获取spring的Ioc核心容器,并根据id获取对象
     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//读取配置文件
        //2.根据id即唯一标志获取Bean对象
        IAccountService as = ac.getBean("accountService", IAccountService.class);
        System.out.println(as);
        as.saveAccount();
    }
}

作用范围

 * 用于改变作用范围的
 *         他们的作用就和在bean标签中使用scope属性实现的功能是一样的
 *         Scope
 *              作用:用于指定bean的作用范围
 *              属性:value:指定范围的取值。常用取值:singleton(单例) prototype(多例) 默认单例。写在类上



@Component("accountService")
@Scope("protoype")
public class AccountServiceImpl implements IAccountService {
    //@Autowired//会自动按照类型注入,会到spring容器中找value中的AccountDaoImpl
    //@Qualifier("accountDao1")//需要和@Autowired一起用*/

    @Resource(name="accountDao1")
    private IAccountDao accountDao;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值