Data preparation:

Define the interface:

/** * define an interface *@author lizehao
 * @companyContinuous payment@dateFebruary 18, 2019 2:54:03 PM */
public interface UserService {}Copy the code

The implementation class

/** * The name of the first implementation class * bean defaults to the full class name beginning with a lowercase userServiceImpl *@author lizehao
 * @companyContinuous payment@dateFebruary 18, 2019 2:54:49 PM */
@Service
public class UserServiceImpl implements UserService {}/** * The name of the second implementation class * bean defaults to the first letter of the full class name, userServiceImpl2 *@author lizehao
 * @companyContinuous payment@dateFebruary 18, 2019 3:00:29 PM */
@Service
public class UserServiceImpl2 implements UserService {}Copy the code

Controller:

/ * * *@author lizehao
 * @companyContinuous payment@dateFebruary 18, 2019 3:22:10 PM */
@Controller
public class UserController {
 
	/** * type injection * If an interface has two implementation classes, the assembly by type error, need to specify the name ** /
	@Autowired
	@Qualifier("userServiceImpl2")
	/** * If an interface has two implementation classes,@ResourceIf the name attribute is specified, the assembly will only be performed by name. * * /
	@Resource(name = "userServiceImpl")
	private UserService userService;
 
	@PostConstruct
	public void init(a) {
		System.out.println("-- -- -- -- -- -- -- -- -- --"+ userService); }}Copy the code
  • Both @autoWired and @Resource can be used to assemble beans.
  • @autowired belongs to Spring; The @resource is a comment on the JSR-250 standard and belongs to J2EE. Inject @Inject is implemented by the JSR330 specification and additional packages need to be imported

@Autowired

By default, the dependent object must exist by default. To allow null values, you can set its required property to false, as in: @autoWired (required=false), if we want to use name assembly we can use it with the @qualifier annotation as follows:

@Autowired
@Qualifier("userServiceImpl")
Copy the code
  • If an interface has two implementation classes, an error will be reported when assembling by type, and the name needs to be specified, as shown in the following example:

@Resource

  • Injection by name (assemble by type if no matching bean is found by name)
@Resource
privateUserService userService; Substance is injected by type@Resource
privateUserService userServiceImpl; Injection by nameCopy the code
  • If an interface has two implementation classes, the @resource will be injected by name by default. If an interface has two implementation classes, the @resource will be injected by name by default
@Resource(name = "userServiceImpl")
Copy the code
  • If the name attribute is specified, it will only be assembled by name.

@autowired is different from @inject

  • @Autowired and @Inject are both assembled by type. The difference is that @Autowired defaults to require=true, while @Inject does not have this property.
  • To match by Name, @autoWired must be used together with @Qualifier and @Inject with @name.