簡(jiǎn)述企業(yè)網(wǎng)站建設(shè)的流程百度推廣培訓(xùn)班
第四章 IOC操作bean管理(基于注解方式創(chuàng)建對(duì)象,注入屬性),完全注解開(kāi)發(fā)
1.IOC操作bean管理(基于注解方式)
(1)什么是注解:
①注解是代碼特殊標(biāo)記,格式:@注解名稱(chēng)(屬性名稱(chēng)=屬性值,屬性名稱(chēng)=屬性值…)
②使用注解,注解作用在類(lèi)上面,方法上面,屬性上面
③使用注解目的:簡(jiǎn)化XML配置。
(2)spring針對(duì)bean管理中創(chuàng)建對(duì)象提供注解。
①@Conponent
②@Service
③@Controller
④@Repository
上面的四個(gè)注解功能是一樣的,都可以用來(lái)創(chuàng)建bean實(shí)例。
2.基于注解方式實(shí)現(xiàn)對(duì)象創(chuàng)建
第一步:引入依賴(lài);
第二步:開(kāi)啟組件掃描;
<!--開(kāi)啟組件掃描如果掃描多個(gè)包,多個(gè)包使用逗號(hào)隔開(kāi)--><context:component-scan base-package="dao,service"></context:component-scan>
第三步:創(chuàng)建類(lèi),在類(lèi)上面添加創(chuàng)建對(duì)象注解;
/**注解里面的value屬性值可以寫(xiě),可以省略不寫(xiě)
默認(rèn)值是類(lèi)名稱(chēng),首字母小寫(xiě)*/
@Component(value = "userService") //<bean id="" class=""/>
public class UserService {public void add(){System.out.println("service add...");}
}@Testpublic void test1(){ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");UserService userService = context.getBean("userService", UserService.class);System.out.println(userService);userService.add();}
3.開(kāi)啟組件掃描細(xì)節(jié)配置:
<!--示例1 不使用默認(rèn)filter,自己配置filterinclude-filter 設(shè)置掃描哪些內(nèi)容目前只掃描帶Controller注解的類(lèi)--><context:component-scan base-package="dao,service" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--示例2下面配置掃描包所有內(nèi)容context:exclude-filter:設(shè)置哪些內(nèi)容不進(jìn)行掃描目前,除了Controller,其他內(nèi)容都掃描 --><context:component-scan base-package="dao,service" ><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>
4.基于注解方式實(shí)現(xiàn)屬性注入:
//XML中只有如下配置<context:component-scan base-package="dao,service"></context:component-scan>
(1)@AutoWired:根據(jù)屬性類(lèi)型進(jìn)行自動(dòng)裝配
第一步:把service和dao對(duì)象創(chuàng)建,在service和dao類(lèi)添加創(chuàng)建對(duì)象注解。
第二步:在service注入dao對(duì)象,在service類(lèi)添加dao類(lèi)型屬性,在屬性上面使用注解。
@Service(value = "userService") //<bean id="" class=""/>
public class UserService {//定義dao類(lèi)型屬性,不需要添加set方法//添加注入屬性注解@Autowiredprivate UserDao userDao;public void add(){System.out.println("service add...");userDao.add();}
}
@Repository
public class UserDaoImpl implements UserDao {@Overridepublic void add() {System.out.println("dao add ...");}
}
(2)@Qualifier:根據(jù)屬性名稱(chēng)進(jìn)行注入
@Qualifier注解的使用,要和@Autowired一起使用。
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {@Overridepublic void add() {System.out.println("dao add ...");}
}
@Service(value = "userService") //<bean id="" class=""/>
public class UserService {//定義dao類(lèi)型屬性,不需要添加set方法//添加注入屬性注解@Autowired@Qualifier(value = "userDaoImpl1")//根據(jù)名稱(chēng)進(jìn)行注入private UserDao userDao;public void add(){System.out.println("service add...");userDao.add();}
}
(3)@Resource:可以根據(jù)屬性類(lèi)型注入,也可以根據(jù)屬性名稱(chēng)注入
//是javax.annotation.Resource中的注解// @Resource //根據(jù)類(lèi)型注入@Resource(name= "userDaoImpl1") //根據(jù)名稱(chēng)注入private UserDao userDao;
(4)@Value:注入普通類(lèi)型屬性
@Value(value = "abc")private String name;
5.完全注解開(kāi)發(fā):
(1)創(chuàng)建配置類(lèi),替代XML配置文件。
@Configuration //作為配置類(lèi),替代配置文件
@ComponentScan(basePackages = {"com"})
public class SpringConfig {
}
(2)編寫(xiě)測(cè)試類(lèi)
@Testpublic void test2(){//加載配置類(lèi)ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);UserService userService = context.getBean("userService", UserService.class);System.out.println(userService);userService.add();}