他们的作用就和在XML配置文件中编写一个标签实现的功能是一样的
Component:
作用: 用于把当前类对象存入spring容器中属性: value:用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母改小写。Controller:一般用在表现层Service:一般用在业务层Repository:一般用在持久层以上三个注解他们的作用和属性与Component是一模一样。他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰
他们的作用就和在xml配置文件中的bean标签中写一个标签的作用是一样的
他们的作用就和在bean标签中使用scope属性实现的功能是一样的
Scope 作用: 用于指定bean的作用范围属性: value:指定范围的取值。常用取值:singleton prototype他们的作用就和在bean标签中使用init-method和destroy-methode的作用是一样的
PreDestroy: 用于指定销毁方法PostConstruct: 用于指定初始化方法bean.xml
<?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" 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"> <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为context名称空间和约束中--> <context:component-scan base-package="com.hzuxy"></context:component-scan> </beans>AccountDaoImpl.java
package com.hzuxy.dao.impl; import com.hzuxy.dao.IAccountDao; import org.springframework.stereotype.Repository; import javax.annotation.Resource; /** * 账户的持久层实现类 */ @Repository("accountDao") public class AccountDaoImpl implements IAccountDao { @Resource(name = "accountDao2") private IAccountDao accountDao = null; public void saveAccount(){ System.out.println("保存了账户"); } }AccountServiceImpl.java
package com.hzuxy.service.impl; import com.hzuxy.service.IAccountService; import org.springframework.stereotype.Service; import java.util.Date; @Service("accountService") public class AccountServiceImpl implements IAccountService { public void saveAccount() { } }配置简单,维护方便(我们找到类,就相当于找到了对应的配置)。
修改时,不用改源码。不涉及重新编译和部署。