【Spring】註解開發

使用註解須知:
必須導入aop的包
在這裏插入圖片描述
要導入context約束,增加註解的支持!

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>
</beans>

1:bean

@Component:組件,放在類上,說明這個類被Spring管理了,就是bean!

package com.lrx.pojo;
import org.springframework.stereotype.Component;

@Component //相當於xml文件中的<bean id="user" class="com.lrx.pojo.User"/>
public class User {
    public String name="孫尚香";
}

2:屬性如何注入

@Value:可以放在屬性上也可以放在set方法上!

package com.lrx.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component //相當於xml文件中的<bean id="user" class="com.lrx.pojo.User"/>
public class User {
    @Value("sunquan") //相當於<property name="name" value="sunquan"/>
    public String name;
}

3:衍生的註解

@Component有幾種衍生註解,我們在web開發中,會按照MVC三層架構分層!
dao【@Repository】
service【@Service】
controller【@Controller】
這四個註解功能都是一樣的,都是代表某個類註冊到Spring中,裝配bean

4:作用域

package com.lrx.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component //相當於xml文件中的<bean id="user" class="com.lrx.pojo.User"/>
@Scope("ptototype") //原型
public class User {
    @Value("sunquan") //相當於<property name="name" value="sunquan"/>
    public String name;
}

5:自動裝配

package com.lixin.pojo;

public class Cat {
    public void shout(){
        System.out.println("喵喵!");
    }
}
------------------------------------------------------------
package com.lixin.pojo;

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

--------------------------------------------------------------
import org.springframework.beans.factory.annotation.Autowired;

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

    public Cat getCat() {
        return cat;
    }
    public Dog getDog() {
        return dog;
    }
    public String getName() {
        return name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

-------------------------------------------------------------------------
<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

        <bean id="cra" class="com.lixin.pojo.Cat"/>
        <bean id="dog" class="com.lixin.pojo.Dog"/>
        <bean id="people" class="com.lixin.pojo.People"/>
    <context:annotation-config/>
</beans>

-----------------------------------------------------------------------------
import com.lixin.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext
                ("ApplicationContext.xml");
        People people=context.getBean("people",People.class);
        people.getDog().shout();
        people.getCat().shout();
    }
}

@Autowired
直接在屬性上使用即可!也可以在set上使用
使用Autowired我們可以不用編寫set方法,前提是這個自動裝配的屬性在IOC容器中存在且符合名字byName!
總結:
xml更加萬能,適用於任何場合!維護簡單方便。
註解不是自己的類使用不了,維護相對複雜。
xml與註解最佳實踐:
xml用來管理bean
註解只負責屬性的注入
我們在使用的過程中,只需要注意一個問題:必須讓註解生效就需要開啓註解的支持

<!--指定要掃描的包,這個包下的註解就會生效-->
    <context:component-scan base-package="com.lrx"/>
    <context:annotation-config/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章