@Autowired注解,但是我们已经使用xml配置来配置bean并将其注入到容器中,但是如果您使用@Component、@Service、@Repository和@Controller注解并启用组件自动扫描,spring会自动将这些bean导入到容器中容器,您不必在 xml 文件中明确定义它们。 所以基本上所有 4 个注解都用于将 bean 定义注册到 Spring 的应用程序上下文中。
@Component 是bean 定义的通用注解,并在应用程序上下文中注册。
@Service
@Service 是专门的组件注解,用于注解属于服务层的类。
@Repository 注解是专门的组件注解,用于在 DAO 层对类进行注解。它还使未经检查的异常有资格转换为 Spring DataAccessException。
@Controller
除非您确定它不属于@Service、@Repository 和@Controller 注释,否则不应使用@Component 注释。
只有当您在 applicationcontext.xml 中使用context:component-scan时,所有这些注释才会起作用。它基本上扫描以上 4 个带注释的类,并使用 Spring 应用程序上下文注册 bean。
<context:component-scan base-package="org.arpit.java2blog" />
在这个例子中,我们将使用上面的注解在控制器、服务和 Dao 类的帮助下创建国家对象。 让我们首先创建我们的 bean 类 Country.java
package org.arpit.java2blog.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.arpit.java2blog.controller; import org.arpit.java2blog.bean.Country; import org.arpit.java2blog.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.arpit.java2blog.service; import org.arpit.java2blog.bean.Country; import org.arpit.java2blog.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.arpit.java2blog.dao; import org.arpit.java2blog.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.arpit.java2blog" /> </beans>
SpringApplicationMain.java
package org.arpit.java2blog.main; import org.arpit.java2blog.bean.Country; import org.arpit.java2blog.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, 2016 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 2016]; root of context hierarchy Aug 08, 2016 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
原文链接:https://codingdict.com/