spring overview

spring overview

實現(xiàn)

實體類定義

@Bean
public class Blog {
    private Integer id;
    private String title;
    private String[] comments;
    private Person author;
    private List<Person> follows;
    private Map<String, String> changes;
    private Set<Person> like;
    private Properties info;
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">


    <!-- more bean definitions go here -->
    <bean id="person" class="cn.spring.pojo.Person">
        <property name="name" value="張三"/>
        <property name="id" value="1"/>
    </bean>

    <bean id="blog" class="cn.spring.pojo.Blog">
<!--        第一種注入,普通值注入, 使用value屬性-->
        <property name="title" value="a blog"/>

<!--        第二種注入,bean注入,使用ref屬性-->
<!--        <property name="author" ref="person"/>-->
        <property name="author">
            <bean class="cn.spring.pojo.Person">
                <property name="id" value="2"/>
                <property name="name" value="李四"/>
            </bean>
        </property>
<!--        第三種注入,數(shù)組注入,使用array value標簽組-->
        <property name="comments">
            <array>
                <value>this is a comment</value>
                <value>this is a comment</value>
                <value>this is a comment</value>
                <value>this is a comment</value>
            </array>
        </property>

<!--        第四中注入,列表注入,使用list標簽 其中嵌套bean標簽或者value標簽-->
        <property name="follows">
            <list>
                <bean id="person1" class="cn.spring.pojo.Person">
                    <property name="id" value="3"/>
                    <property name="name" value="王五"/>
                </bean>
                <bean id="person2" class="cn.spring.pojo.Person">
                    <property name="id" value="4"/>
                    <property name="name" value="田六"/>
                </bean>
            </list>
        </property>

<!--        第五種注入,map注入,使用map entry標簽對-->
        <property name="changes">
            <map>
                <entry key="第一次改動" value="改動內(nèi)容"/>
                <entry key="第二次改動" value="改動內(nèi)容"/>
            </map>
        </property>

<!--        第六種注入,set注入, 使用set標簽-->
        <property name="like">
            <set>
                <ref bean="person"/>
            </set>
        </property>

<!--        第七種注入,空值注入,一種使用null標簽,第二種將value值設(shè)為空串就行-->
<!--        <property name="id">-->
<!--            <null/>-->
<!--        </property>-->
        <property name="id" value=""/>

<!--        第八中輸入,property注入,使用proos 和prop標簽對-->
        <property name="info">
            <props>
                <prop key="link">www.baidu.com</prop>
            </props>
        </property>
    </bean>
</beans>

結(jié)果

    @Test
    public void iocTest() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Blog blog = applicationContext.getBean(Blog.class);
        /*
        * Blog
        * (id=null,
        *  title=a blog,
        *  comments=[this is a comment, this is a comment, this is a comment, this is a comment],
        *  author=Person(id=2, name=李四),
        *  follows=[Person(id=3, name=王五), Person(id=4, name=田六)],
        *  changes={第一次改動=改動內(nèi)容, 第二次改動=改動內(nèi)容},
        *  like=[Person(id=1, name=張三)],
        *  info={link=www.baidu.com})
        * */
        System.out.println(blog);
    }

注意點

bean注入

使用bean注入可以使用兩種方法

  • 一是使用自閉和標簽用ref屬性來指定一個定義過的bean
<property name="author" ref="person"/>
  • 第二種就是使用內(nèi)嵌的bean子標簽,值得注意的是,此時定義的這個bean并不會能被其他bean所調(diào)用,類似于java中匿名內(nèi)部類(就算你在這個bean標簽內(nèi)使用id和name屬性來命名,在外部也是無法檢測到的)至于它是實現(xiàn)方法是不是就是使用java中的匿名內(nèi)部類,由于沒看過spring源碼就不去猜測了。

set list array 和 map 下的entry

這幾個標簽下都可以嵌套根據(jù)你在實體類中的定義嵌套使用 value, bean, ref, idref, props, null以及他們自生這幾個標簽,總之就是對應(yīng)java類進行出力

空值注入

控制注入也有兩種方式,但是這兩種方式所得到的結(jié)果是不一樣的!

  • 一是使用<null/>標簽
  • 而是是value的值為空字符串,即:<property name="id" value=""/>
    他們兩個在String類型下得到結(jié)果自然是一一對應(yīng)的""和null。而如果為其他java基本數(shù)據(jù)類型的話兩個都為null。如果為其他類型使用空串進行注入則會報錯。其中byte類兩種方式都會報錯,暫時還不清楚它具體的判斷邏輯。

spring注解開發(fā)

使用注解配置

@Configuration 使用該注解是一個類變成一個spring配置類。該注解本身也是一個Component容器,會將類掃描到spring容器中。

  • 配合的注解:
    • @ComponentScan指定掃描的包名
    • @Bean注冊beans
    • @Import合并多個配置類

掃描

``

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

    <context:annotation-config/>
    <context:component-scan base-package="xxx"/>
</beans>

自動注入

@Autowried

  • @Autowired 可以在屬性上使用,也可以在set方法上使用。
    可以配合required屬性或者@Nullable注解使用使得這個對象可以為null
  • 配合@Qualifier(value="bean")使用可以指定哪一個具體的bean作為注入對象
  • @Resource(name="bean") 的效果類似于@Autowired的效果,但是@Autowired是byType選擇,配合 @Qualifier可以做到byName。而@Resource(name="bean")默認byName,如果找不到,還會繼續(xù)用byType進行查找。

@Component

@Component 該注解下的類都會被掃描到Ioc容器中配合@Value可以對bean進行配置,與之有相同功能的還有@Service @Controller @Repository他們都是@Component的別名,有這一項的效果,一般用于幫助程序員區(qū)分各個層級。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
    String value() default "";
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}


@Scope

指定bean的作用域,有prototype, singleton,request,session,application, websocket 這六種(后四種為webAppliction專屬)。 默認為單例模式(singleton)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容