Spring-02(set注解,p,c命名,自动装配,注解实现自动装配,Component注解以及其衍生注解)

依赖注入DI:

依赖是指:Bean对象的创建依赖于容器。注入是指:Bean对象中的所有属性,由容器来注入。

一些常见的Set注入方法:

Student对象代码(get,set,tostring我就不写了,肯定要有的):

package pojo;

import java.util.*;

public class Student {
    private  String name;
    private  Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String wife;

}

xml中注入方法:

<bean id="address1" class="pojo.Address">
        <property name="address" value="河南省"/>
    </bean>
    <bean id="student" class="pojo.Student">
<!--        普通值注入-->
        <property name="name" value="许晨阳"/>
<!--        Bean注入-->
        <property name="address" ref="address1"/>
<!--        数组注入-->
        <property name="books">
            <array >
                <value>红楼梦</value>
                <value>西游记</value>
                <value>三国演义</value>
            </array>
        </property>
<!--        list-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>
<!--        map-->
        <property name="card">
            <map>
                <entry key="身份证" value="12345"/>
                <entry key="银行卡" value="54321"/>
            </map>
        </property>
<!--        set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>云顶之弈</value>
            </set>
        </property>
<!--        null-->
        <property name="wife">
            <null/>
        </property>
<!--        property-->
        <property name="info">
            <props>
                <prop key="学号" >41724041</prop>
                <prop key="身高">180</prop>
            </props>
        </property>
    </bean>

p命名注入:

①先在xml中加入

xmlns:p="http://www.springframework.org/schema/p"

②展示一下java代码(get,set,tostring自己写)

public class User {
    private String name;
    private int age;
}

③set注入和p注入的区别。

<!--set注入-->
    <bean id="user" class="pojo.User">
        <property name="name" value="xcy"/>
        <property name="age" value="23"/>
    </bean>
<!--    p命名空间注入-->
    <bean id="user" class="pojo.User" p:age="23" p:name="xcy"/>

 

c命名注入

加入的xml约束和c注入的方式和p命名都一样。只是把p换成c就行了

两者的注意点:
①p命名和c命名都需要导入约束:

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

②p命名对应着set注入,c命名对应着构造器注入,前者需要无参构造函数,后者需要有参构造函数。

Bean自动装配:

一般都用byType和byName,代码如下:

    <bean id="cat" class="pojo.Cat"></bean>
    <bean id="dog" class="pojo.Dog"></bean>
    <bean id="people" class="pojo.People" autowire="byName">
        <property name="name" value="xuchen"/>
    </bean>

①byName根据的是people里面的set方法后面的值,比如我的代码people中:

    public void setCat(Cat cat) {
        this.cat = cat;
    }

set后面是Cat,那么自动装配cat属性值时,应为cat,大小写不区分,如果是SetCat1,那么id就要是cat1.

②byType根据的是类型自动装配,如果有两个一样的类型,就不能用byType,并且不需要id,也能装配,代码如下:

    <bean class="pojo.Cat"></bean>
    <bean class="pojo.Dog"></bean>
    <bean id="people" class="pojo.People" autowire="byType">
        <property name="name" value="xuchen"/>
    </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
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

②配置注解的支持:<context:annotation-config/>(上一段代码已经加上)

③编写xml

        <bean id="cat" class="pojo.Cat"></bean>
        <bean id="dog" class="pojo.Dog"></bean>
        <bean id="people" class="pojo.People" />

④在java文件中需要自动装配的属性前加上@Autowired,比如cat和dog需要自动装配

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
}

⑤注解使用反射实现的,可以不用set方法。Autowired默认先按byType,如果发现找到多个bean,则,又按照byName方式比对,如果还有多个,则报出异常(可以用@Qualifier(value="id")来指定)

⑥@Resource也可以实现自动注解,但是他是先找名字再找类型,和@Autowired相反,@Resource也可以指定 @Resource(name="")

Component注解以及其衍生注解:

①导入约束并添加注解支持:

<?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
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="pojo,dao"/>
    <context:annotation-config/>
</beans>

② 注解需要用到的类

@Component
public class User {
    public String name="许晨阳";

}



@Component
public class User {
    @Value("许晨阳")
    public String name;
    @Value("14")
    public int age;

}

③注解类后的id就是字母小写,比如第二步注解的是User,那么可以通过User user = Context.getBean("user", User.class);获取Bean对象。

④component的衍生注解,在web开发中,会按照MVC三层架构分层。

1)Dao层。@Repository

2)Service层。@Service

3)controller层。@Controller

这三个注解和component注解功能都是一样的,都是代表某个类注入到Spring中,装配Bean。

posted @ 2022-03-16 21:13  塵暘  阅读(96)  评论(0)    收藏  举报