SpringBean配置之注解方式

1.在pom.xml引入Spring-aop依赖

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.3.4</version>
    </dependency>

2.开启注解的包扫描
①引入新的命名空间以及schema的文件
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html中搜索xmlns:context,然后复制三行代码到上面beans的配置上,内容如下:

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

    <context:annotation-config/>

</beans>

②识别包注解:在applicationContext.xml中指明哪些包将使用注解

<context:component-scan base-package="org.csu.spring.demo.ioc"/>

③进行注解
常用的注解:

//@Component("account"):一般意义的bean注解
//@Repository:用于对DAO实现类的注解
//@Service:用于对Service业务逻辑类的注解
//@Controller:用于对Controller控制器的注解

注解方式的设置注入:

//    注解方式的设置注入
    @Value("Mike")
    private String username;
    @Value("666")
    private String password;
    @Value("18")
    private int age;

注入对象的注解:

//注入对象的注解
    //①Autowired按类型去搜索
    @Autowired
    //②按名称去指定
//    @Resource(name = "accountDAO")
    private AccountDAO accountDAO;

3.测试

import org.csu.spring.demo.ioc.Service.AccountService;
import org.csu.spring.demo.ioc.domain.Account;
import org.csu.spring.demo.ioc.persistence.AccountDAO;
import org.csu.spring.demo.ioc.persistence.AccountDAOImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
    @Autowired
    private AccountService service;

    @Test
    public void test() {
        //一、推荐方式:
//        ①:添加注入
//            <dependency>
//                <groupId>org.springframework</groupId>
//                 <artifactId>spring-test</artifactId>
//                 <version>5.3.4</version>
//             </dependency>
//            ②:添加注入标识
//        @RunWith(SpringJUnit4ClassRunner.class)
//        @ContextConfiguration("classpath:applicationContext.xml")
//            ③:添加bean
//        @Autowired
//        private AccountService service;

        //二、传统方式:
        //初始化spring的环境,然后就可以使用spring的容器,就可以使用工厂创建对象(使用Spring IoC容器来获取对象)
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//        Account temp = (Account) context.getBean("account");
//        System.out.println(temp.getUsername() + "," + temp.getPassword() + "," + temp.getAge());

//        AccountService accountService = (AccountService) context.getBean("accountService");
//        accountService.login();
        service.login();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZenSheep

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值