In doing the project, the project found that loaded the class, some places use @Autowired, some places use @Resource
In the Internet to collect information on
Common
@Resource @Autowired and can be used as a modified injection property, when the interface implementation class only a single, same modification effect two annotations may be replaced with each other, does not affect.
difference
Java is @Resource own annotations, @ Resource has two attributes are more important, points that name and type; Spring @Resource annotation name will resolve to the bean attribute name, and the type attribute is resolved to the type of bean. So if you use the name attribute is used byName automatic injection strategy, and is used when using the type attribute byType automatic injection strategy. If neither type attribute name is assigned, the automatic injection strategy case byName by using reflection.
@Autowired is spring annotations spring2.5 version is introduced, Autowired only injection according to type, not to match the name. If it comes to the type injection can not distinguish objects that need to rely on @Qualifier or modified @Primary notes together.
Write Liezi
New HumanService.java class
package com.komiles.study.service; /** * @author [email protected] * @date 2020-03-23 11:46 */ public interface HumanService { /** * 跑马拉松 * @return */ String runMarathon(); }
Implementation class ManServiceImpl.java
package com.komiles.study.service.impl; import com.komiles.study.service.HumanService; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; /** * @author [email protected] * @date 2020-03-23 11:48 */ @Service public class ManServiceImpl implements HumanService { /** * 跑马拉松 */ @Override public String runMarathon() { return " A man run marathon"; } }
New HumanController.java
package com.komiles.study.controller; import com.komiles.study.service.HumanService; import com.komiles.study.service.impl.ManServiceImpl; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author [email protected] * @date 2020-03-23 11:49 */ @RestController @RequestMapping("/human") public class HumanController { @Autowired private HumanService humanService; @GetMapping("/run") public String runMarathon() { return humanService.runMarathon(); } }
Run the program
Output content: man run marathon
The controller in the @Autowired into @Resource can normally access.
If I write more implementation classes what will happen?
Create a WomanServiceImpl.java
package com.komiles.study.service.impl; import com.komiles.study.service.HumanService; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; /** * @author [email protected] * @date 2020-03-23 12:01 */ @Service public class WomanServiceImpl implements HumanService { /** * 跑马拉松 */ @Override public String runMarathon() { return "A Woman run marathon"; } }
Run the program, we found an error, because there are two implementation classes, to find that the program does not know
How to do it?
There are two ways
The first, in the implementation class to the class from the name, the name at the time of the introduction of direct introduction.
For example: bonus on ManServiceImpl.java class, @ Service. @Service (value = "manService") or @Component (value = "manService")
package com.komiles.study.service.impl; import com.komiles.study.service.HumanService; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; /** * @author [email protected] * @date 2020-03-23 11:48 */ @Service(value = "manService") //@Component(value = "manService") public class ManServiceImpl implements HumanService { /** * 跑马拉松 */ @Override public String runMarathon() { return " A man run marathon"; } }
When used in Controller class, but also the need to develop what are your names.
If you use @Resource need to add @Resource (name = "manService")
If you use @Autowired need to use @Qualifier (value = "manService")
package com.komiles.study.controller; import com.komiles.study.service.HumanService; import com.komiles.study.service.impl.ManServiceImpl; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author [email protected] * @date 2020-03-23 11:49 */ @RestController @RequestMapping("/human") public class HumanController { @Autowired @Qualifier(value = "manService") // @Resource(name="manService") private HumanService humanService; @GetMapping("/run") public String runMarathon() { return humanService.runMarathon(); } }
If you want to reference a particular priority class, you can use @Primary on the implementation class.