Spring xml配置文件和常用注解学习笔记

Spring的运用目的,用来管理bean。利用IOC管理 Bean。
IOC实现的两种方式使用XML和注释的对象比
AccountServiceImpl.java

/**
 * 账户业务层实现类
 *
 *  曾经的XML配置方法
 *     <bean id="accountService" class="com.Ryuu.service.impl.AccountServiceImpl"
 *          scope="" init-method="" destory-method="">
 *          <property name="" value="" /ref=""
 *     </bean>
 *
 *  用于创建对象
 *     xml <bean>
 *     @Component   【组成部分】
 *          把当前对象类存入Spring容器中
 *          Spring容器key-value的Map存储
 *          属性:value 用于指定bean的id,默认值 类名 首字母小写
 *     @Controller:一般用在表现层
 *     @Service:一般用在业务层
 *     @Repository:一般用于持久层
 *     以上三个注解他们的作用和属性与Compeonet一摸一样
 *     他们三个是Spring框架为我们提供明确的三层架构使用的注解,使我们对三层架构更加清晰
 *  用于注入数据
 *  xml <bean> -> <property></property>
 *     @Autowried:
 *     作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
 *     出现位置:可以是变量,可以是方法
 *     @Qualifier
 *     作用:在按照类型注入的基础上,在按照名称注入
 *          在给类成员使用是不可以单独使用
 *          但给方法参数使用时可以
 *     属性:value 用于指定注入bean的Id
 *     @Resource
 *     作用:直接按照bean的Id注入,可以单独使用
 *     属性:name 用于指定bean的Id
 *     ✳以上三个注解只能用于bean类型,不能用于string和基本类型
 *       集合类型的注入通过xml实现
 *     @Vaule
 *     作用:用于注入String 和 基本类型
 *     属性:value 用于指定数值 可以使用SpringEl表达式 ${表达式}
 *  用于改变作用范围
 *     @Scop
 *     作用:用于指定bean的作用范围
 *     属性:value singleton prototype
 *  xml <bean> -> <scope></scope>
 *  生命周期
 *  xml <bean> -> init-method destroy-methode
 *  PreDestroy
 *  用于指定销毁方法
 *  PostConstruct
 *  用于指定初始化方法
 */
@Component(value = "AccountService")
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao;
    public AccountServiceImpl(){
        System.out.println("对象创建了");
    }
    public void saveAccount() {
        accountDao.saveAccount();
    }
}

bean.xml
xmlns:context="http://www.springframework.org/schema/context"的引入告诉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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--告知spring在创建容器时要扫描的包,配置标签不是bean中的约束,而是一个名称为context名称的空间和约束中-->
    <context:component-scan base-package="com.Ryuu"></context:component-scan>
    <context:annotation-config/>

</beans>

主方法呼叫

public class Client {

    /**
     * 获取Spring的Ioc核心容器 根据ID获取对象
     *
     */
    public static void main(String[] args) {

        //1.获取核心容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService)ac.getBean("AccountService");
        System.out.println(as);

        IAccountDao adao = (IAccountDao)ac.getBean("AccountDao");
        adao.saveAccount();
        System.out.println(adao);
    }
}

遇到的问题
在运行试验的时候遇到了一下错误
Post-processing of merged bean definition failed; nested exception is java.lang.NoSuchMethodError: ‘java.lang.String javax.annotation.Resource.lookup()’

查了半天发现是在写pom文件的时候版

    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>

分别在不同的时间点写过1.3.1 和 1.3.2两个版本,这样maven就下载了两个包
在这里插入图片描述
删掉多余的就ok

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值