(1)導入AOP的jar,因爲註解的後臺實現用到了AOP編程
(2)更換配置文件頭,即添加相應的約束(添加context約束,即context=“" xsi:schemaLocation="…spring-context.xsd”)
設置掃描包路徑
<context:component:scan base-package=""/>
@Component(“myStudent”) //表明當前類爲組件,容器創建的這個組件對象名稱爲myStudent,相當於bean的id屬性
與本註解具有相同功能的還有三個:
@Repository:註解在Dao接口的實現類上,表示當前Dao類爲組件,
,@Service:註解在Service接口的實現類上,表示當前Service類爲組件,
@Controller:註解在Controller類上,表示當前Controller類爲組件,
@Scope("property”) //設置bean的作用範圍,默認singleton
實例
@Component("myStudent")
@Scope("property”)
public class Student{
@Value("張三") //爲name屬性賦值
private String name;
//爲對象屬性賦值
//1.@resource //jdk裏的,不是spring的註解 這是byType方式自動注入
//2.@resource(name="mySchool") //School類上配了Component("mySchool"),這是byName方式自動注入
//3.@Autowired //spring的註解 這是byType方式自動注入
//4.@Qualifier("mySchool") //byName,4必須和3一起用
private School school;
//@Value("張三") //也可以放在set方法上
public void setName(String name){
this.name = name
}
public String getName(){
return name
}
...
}
Bean的生命週期始末註解
@postConstruct
@preDestroy
2.使用javaConfig進行配置(瞭解)
自己寫容器(46以後)