当前位置:网站首页>Autowired usage
Autowired usage
2022-06-23 02:51:00 【Spicy drunk shrimp】
Recommended documents :Spring @Autowired annotation _w3cschool
Usage cases
One 、 Interface , In the interface, a save Method
package com.example.autowired;
public interface UserRepository {
void save();
}Two 、 Implementation interface , Override methods in the interface
package com.example.autowired;
import org.springframework.stereotype.Repository;
@Repository("userRepository")
public class UserRepositoryImps implements UserRepository{
@Override
public void save() {
System.out.println(" Love to eat lollipops ");
}
}3、 ... and 、Autowired annotation , There are three forms of annotation , They are annotation fields 、 Constructors 、setting.
package com.example.autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.autowired.UserRepository;
@Service
public class UserService {
// The function of this annotation is to use Autowired To annotate the field
// @Autowired
// private UserRepository userRepository;
private final UserRepository userRepository;
// This is to annotate the constructor
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void save(){
userRepository.save();
}
}Four 、 Running results
package com.example.autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class run {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService) ctx.getBean("userService");
userService.save();
}
}Need some xml To configure ,autowired It's through xml Injected .
5、 ... and 、xml To configure
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.example.autowired">
</context:component-scan>
</beans>Running results
22:08:01.334 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' 22:08:01.342 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userRepository' 22:08:01.346 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService' 22:08:01.357 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Autowiring by type from bean name 'userService' via constructor to bean named 'userRepository' Love to eat lollipops
边栏推荐
- Phpexcel export with picture Excel
- February 4, 2022: combined total IV. Give you a number composed of different integers
- PNAs: power spectrum shows obvious bold resting state time process in white matter
- There is no WM data in the primary commodity master data store view of SAP retail
- February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
- Extract NTDs with volume shadow copy service dit
- Ugui empty button implementation
- Exploit format string vulnerability in CDE
- 2022-01-30: minimum good base. For a given integer n, if K (k) of n
- Reinforcement learning series (III) -gym introduction and examples
猜你喜欢

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

Performance test -- Jenkins environment construction for 15jmeter performance test

Xgboost principle

Performance test -- 14 detailed explanation of performance test report and precautions

Small knowledge points of asset

WebService details
What is sitelock? What is the function?

Understand GB, gbdt and xgboost step by step

My good brother gave me a difficult problem: retry mechanism

Microservice Optimization: internal communication of microservices using grpc
随机推荐
Three ways to get class
The difference between script in head and body
PNAs: power spectrum shows obvious bold resting state time process in white matter
Section 6: basic configuration I of spingboot
Special exercise split line-----------------------------
Capture passwords of all chrome versions
[data preparation and Feature Engineering] data cleaning
What is sitelock? What is the function?
Analysis of resolv Conf common parameters
Pnas: amygdala individual specific functional connectivity: Fundamentals of precision psychiatry
A bit about the state machine (FSM SMR DFSM)
Deep analysis of time complexity
Quick sorting C language code + auxiliary diagram + Notes
Biological psychiatry: defining individualized functional neuroanatomy for precision psychiatry
Some people are crazy, others are running away, and open source software is both hot and cold
What if the software gets stuck and cannot end the process?
February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
Buuctf misc-[actf freshman competition 2020]outline
New uniapp+uniui background management uniuadmin
51. numerical arrangement