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