Spring:注解JDK类型注入、《排除和包含》

 

基于JDK类型的注入:

     因为他不知道properties中key值对应的value是数组还是list

为集合类型进行注入

init.properties:

id =10
name=suns

 applicationContext.xml:中写入

<context:property-placeholder location="classpath:init.properties"/>
<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


<!--
  id:名字唯一
  class属性:类的全限定名
  通过写bean标签告诉工厂生产的对象
-->

<!--    让spring框架在设置的包及其子包中扫描对应的注解,使其生效-->
    <context:component-scan base-package="com.baizhiedu"/>

<!--    通过配置文件可以覆注解的内容-->
    <bean id="u" class="com.baizhiedu.bean.User">
        <property name="id" value="10"/>
    </bean>


<!--    读取配置文件-->
    <!--<context:property-placeholder location="classpath:init.properties"/>-->
</beans>

Category:

package com.baizhiedu.injection;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component//注解指定创建的对象
@PropertySource("classpath:/init.properties")//注解替换在配置文件中的:<context:property-placeholder location="classpath:init.properties"/>标签
public class Category {
    @Value("${id}")//加入Value注解为id属性新型注入赋值
    private Integer id;
    @Value("${name}")
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 JDKAnnotation

package com.baizhiedu;

import com.baizhiedu.injection.Category;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JDKAnnotation {
    //测试JDK注入 Value注入
    @Test
    public void test1(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Category category=(Category)ctx.getBean("category");
        System.out.println("category.getId()="+category.getId());
        System.out.println("category.getName="+category.getName());

    }
    //测试spring注入产生的对象的id
    @Test
    public void test2(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        String [] beanDefinitionNmaes=ctx.getBeanDefinitionNames();//获得spring工厂的所有对象的id值
        for (String beanDefinitionName: beanDefinitionNmaes){
            System.out.println("beanDefinitionName="+beanDefinitionName);
        }

    }
}

 test1

test2: 

 

包含方式是进行扫描 下面的不进行扫描,改为进行 扫描

 

 

 

 

 applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


<!--
  id:名字唯一
  class属性:类的全限定名
  通过写bean标签告诉工厂生产的对象
-->

<!--    让spring框架在设置的包及其子包中扫描对应的注解,使其生效-->
    <context:component-scan base-package="com.baizhiedu">
        <!--排除特定类型,不进行扫描-->
        <context:exclude-filter type="assignable" expression="com.baizhiedu.bean.User"/>

        <!--排除特定的注解 不进行扫描  下面排除@Service注解的类-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>

         <!-- aspecj类型 后面可以写包作为切入点还可以类作为切入点
         排除特定包及其子包下的类,以包作为切入点:com.baizhidu.injection..*
         以类作为切入点:*..User 不管是哪个包里面只要是User类都排除
         -->
        <context:exclude-filter type="aspectj" expression="com.baizhiedu.injection..*"/>
        <context:exclude-filter type="aspectj" expression="*..User"/>
    </context:component-scan>


<!--    包含方式-->
    <context:component-scan base-package="com.baizhiedu" use-default-filters="false">
<!--        只下面写的注解扫描Resposity、Service注解,只会创建UserDAO对象-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>



<!--    通过配置文件可以覆注解的内容-->
    <bean id="u" class="com.baizhiedu.bean.User">
        <property name="id" value="10"/>
    </bean>


<!--    读取配置文件-->
    <context:property-placeholder location="classpath:init.properties"/>


<!--    Spring注解与配置文件的互通-->
    <bean id="userService" class="com.baizhiedu.injection.UserServiceImpl">
        <!--ref可以使用注解产生的对象-->
        <property name="userDAO" ref="userDAOImpl"/>
    </bean>
</beans>

产生的所有对象:

使用排除方式后:

使用包含方式后:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

喵俺第一专栏

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

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

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

打赏作者

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

抵扣说明:

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

余额充值