Spring @Component,@Service,@Repository和@Controller注释
时间:2020-02-23 14:35:50 来源:igfitidea点击:
我们已经看到了@autowired注释,但我们使用XML配置将Bean配置为配置bean并将其注入容器,但如果使用@component,@service,@repository和@controller注释和启用组件自动扫描,则Spring将自动将这些bean导入容器,我们不必在XML文件中显式定义它们。
所以基本上所有4个注释都用于使用Spring的应用程序上下文注册Bean定义。
@Component:
@Component是Bean定义和应用程序上下文的寄存器的通用注释。
@Service :
@Service是专门的组件注释,用于注释属于服务层的类。
@Repository:
@Repository注释是专门的组件注释,用于在DAO层注释类。
它还使得未选中的异常有资格转换为Spring DataAccessException。
@Controller :
@clersion注释是专门的组件注释,用于在演示层注释类。
它广泛用于Spring MVC应用中。
You should not use @Component annotation unless you are sure that it does not belong to @Service, @Repository and @Controller annotation.
启用组件扫描:
所有这些注释只有在使用上下文时才工作:ApplicationContext.xml中的组件扫描。
它基本上扫描以上4个注释类和注册BEAS,具有Spring应用程序上下文。
<context:component-scan base-package="org.igi.theitroad"
Spring注释示例:
在此示例中,我们将使用上述注释使用控制器,服务和DAO类的帮助来创建国家对象。
允许首先创建我们的bean类country.java
package org.igi.theitroad.bean; public class Country{ String countryName; long population; public Country() { super(); } public Country(String countryName,long population) { super(); this.countryName = countryName; this.population=population; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } }
CountryController,Java.
package org.igi.theitroad.controller; import org.igi.theitroad.bean.Country; import org.igi.theitroad.service.CountryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller ("countryController") public class CountryController { @Autowired CountryService countryService; public Country createNewCountry() { return countryService.createNewCountry(); } }
Countryservice.java.
package org.igi.theitroad.service; import org.igi.theitroad.bean.Country; import org.igi.theitroad.dao.CountryDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("countryService") public class CountryService { @Autowired CountryDAO countryDAO; public Country createNewCountry() { return countryDAO.createNewCountry(); } }
countrydao.java.
package org.igi.theitroad.dao; import org.igi.theitroad.bean.Country; import org.springframework.stereotype.Repository; @Repository("countryDAO") public class CountryDAO { public Country createNewCountry() { //You should get it from database Country country = new Country("Ïndia", 40000); return country; } }
applicationcontext.xml.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="org.igi.theitroad" </beans>
SpringApplicationMain.java.
package org.igi.theitroad.main; import org.igi.theitroad.bean.Country; import org.igi.theitroad.controller.CountryController; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringApplicationMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); CountryController controller = (CountryController) context.getBean("countryController"); Country country = controller.createNewCountry(); System.out.println("Country Name : " + country.getCountryName()); System.out.println("Country's Population : " + country.getPopulation()); } }
运行上面的程序时,我们将得到以下输出:
Aug 08, 2015 12:00:42 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c2709da: startup date [Mon Aug 08 00:00:42 IST 2015]; root of context hierarchy Aug 08, 2015 12:00:42 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationContext.xml] Country Name : Ïndia Country's Population : 40000