Spring基础-注解管理bean

什么是注解及注解作用

1、注解是代码中特殊标记,格式:@注解名称(属性名=属性值, 属性名=属性值 ..)
2、注解使用在类、方法、属性
3、作用:简化xml的配置

常用注解

@Component
@Service
@Controller
@Repository
上述四个注解作用在类上-主要作用是实例化类
@AutoWired 根据属性类型自动注入
@Qualifier 根据属性名称进行注入
@Resource 即可根据属性类型注入 也可以根据属性名称进行注入 javax 扩展包 提供的
上述三个注解作用是注入属性
@value 属性赋值

部分代码如下

目标:
为PersonEntity注入name及sex值,并将PersonEntity注入到PersonServiceImpl类中,然后调用PersonEntity中的add()方法,输出注入的属性值

1、新建PersonEntity类及PersonServiceImpl类

//  这四个注解都可以使用
// @Repository()
// @Service
// @controller
// @Repository
 @Component
public class PersonEntity {

    @Value("张三")
    private String name;

    @Value("男")
    private String sex;

    public void add () {
        System.out.println("执行了PersonEntity,姓名:" + name + "性别:" + sex);
    }

}
@Service
public class PersonServiceImpl {

    @Autowired
    private PersonEntity personEntity;

    public void getPersonEntity() {
        System.out.println("执行PersonServiceImpl.......");
        personEntity.add();
    }
}

2、bean.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:content="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 https://www.springframework.org/schema/context/spring-context.xsd">

        <!--
            开启注解
            base-package:需要扫描类的路径 可以是多个用逗号隔开base-package="work.chenc.entity,base-package="work.chenc.service""
            ase-package="work.chenc.*" * 通配符  表示扫描work.chenc 包下面所有带有注解的类

         -->
        <content:component-scan base-package="work.chenc.*"></content:component-scan>
        
        <!--
            use-default-filters="false" 不扫描work.chenc.*的所有注解
            只扫描content:include-filter 中配置的注解 这里只扫描 Controller 注解
         -->
<!--        <content:component-scan base-package="work.chenc.*" use-default-filters="false">-->
<!--                <content:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
<!--        </content:component-scan>-->

        <!--
            这里的意思是扫描所有类注解
            但Controller注解的类不尽心扫描
         -->
<!--        <content:component-scan base-package="work.chenc.*">-->
<!--                <content:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
<!--        </content:component-scan>-->
</beans>

3、测试

    @Test
    public void testPerson() {
        // 1、获取ApplicationContent对象解析xml文件并创建对象 - spring内部处理
        // 我这里bepersonbean.xml 是我对应的xml文件的名字 也就是上面bean.xml
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:personbean.xml");
        // 2、获取创建的对象
        PersonServiceImpl personServiceimpl = context.getBean("personServiceImpl", PersonServiceImpl.class);
        personServiceimpl.getPersonEntity();
    }

4、执行结果

执行PersonServiceImpl.......
执行了PersonEntity,姓名:张三性别:男

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容