java Spring 注解配置入门

  • 常用依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.3.RELEASE</version>
        </dependency>

        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <!--log4j日志-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

注解说明

  • @Component:放在类上面,用于创建对象,功能相当于配置文件中的bean
  • @Autowired:自动装配,通过类型,名字
    如果Autowired不能唯一装配上属性,则通过@Qualifier(value = “xxx”)
  • @Resource :自动装配通过名字,类型
  • @nullable : 字段标记这个注解后,说明这个字段可以为null

代码演示

cat类

定义一个cat类,在cat类中定义一个shuout方法,并且使用@Component创建对象

@Component
public class Cat {
    public void shout(){
        System.out.println("喵喵喵");
    }
}

Dog类

定义一个dog类,在cat类中定义一个dog方法,并且使用@Component创建对象

@Component
public class Dog {
    public void shout(){
        System.out.println("汪汪汪");
    }
}

people类

定义一个people类,在people类中,定义一个cat类和dog类的对象变量,再定义一个name变量,并且使用@Component创建对象

/*
 * @nullable : 字段标记这个注解后,说明这个字段可以为null
 *
 * @Resource与@Autowired的区别
 *
 *      @Autowired 通过bybame的方法实现,而且必须要求有这个对象存在
 *
 *          @Qualifier 指定@Autowired的名字
 *
 *      @Resource 默认通过byName的方式实现,如果找不到名字,则通过byType方式实现
 *
 *
 * */
public class people {
    //@Autowired(required = false)
    //如果定义的Autowired的required为false,说明这个对象可以为null,否则不允许为空

    //先通过名字查找如果没有再通过类型查找,可指定name
    @Resource
    private Cat cat;
    @Autowired
    //指定装配的
    //@Qualifier
    private Dog dog;
    private String name;


    public Cat getCat() {
        return cat;
    }

    public Dog getDog() {
        return dog;
    }

    public String getName() {
        return name;
    }

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


}

bean配置文件

在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:p="http://www.springframework.org/schema/p"
       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 http://www.springframework.org/schema/context/spring-context.xsd">


    <!--
          autowire : 自动装配
            属性:
                byName :
                        根据名字自动装配,会自动在容器寻找,和自己对象set方法对应点的beanId
                    如Setdog byName就会去找是否有id为dog的bean对象,如果有则自动装配,无则
                    无法自动装配
                byType :
                        根据类型自动装配,会自动寻找和自己对象属性类型相同的bean,注:type要
                    保证只有一个与自己对象属性类型相同的bean,也就是class唯一,否则会报错

    -->
    <context:component-scan base-package="com"/>
<!--    <bean id="cat" class="com.lcf.pojo.Cat"/>-->
<!--    <bean id="dog" class="com.lcf.pojo.Dog"/>-->
    <bean id="people" class="com.lcf.pojo.people" p:name="张三"/>
</beans>

单元测试

@Test
    public void testPeople(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean01.xml");
        people people = context.getBean("people", people.class);
        System.out.println(people.getName());
        people.getCat().shout();
        people.getDog().shout();
    }

测试结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值