(修改完成)spring 梳理6--注解开发 (二) 组件开发 @Component @Value

@Component

@Value

 

 标识一下,这个类是javabean,扫包的时候要扫进去

 

(1)删除beans.xml里面所有的<bean>

<?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
https://www.springframework.org/schema/context/spring-context.xsd
">

    <!--扫包,com.xinzhi底下都扫扫看-->
    <context:component-scan base-package="com.xinzhi"/>


</beans>

 

 

 

(2)类前面加@Component,参数前面加@Value

                             

 

 

 

    

 

 

 

 

注意这里用@Resource

 

 

 

 

 

(3)结果:

User(name=张三, address=Address(addressInfo=南京), hobbies=null, duties=null, familyTies=null, carts=null, workExperience=null, dog=Dog(type=teddy))

 

 

注意:

不是导入这个包:

 

 

 

 是导入这个包:

 

posted @ 2021-01-23 16:04  Master_Sun  阅读(232)  评论(0编辑  收藏  举报