【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/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章