SpringIoC

IOC : Inversion of control, handing over the creation of objects to Spring.
Simply implement the following with code:
write picture description here

write picture description here
Give the creation right of UserDao userDao = new UserDaoImpl(); to Spring, and

then hand over the creation right of UserService to Spring
reference ref='the alias id taken by yourself'
name is the attribute name

创建一个User类:
public class User {
private int age;
private String name;
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return “User [age=” + age + “, name=” + name + “]”;
}
}
创建接口:
public interface UserDao {
public void save(User user);
}
创建接口实现类:
public class UserDaoImpl implements UserDao {

@Override
public void save(User user) {
        System.out.println(user);
}

}
Create Service:
public interface UserService {
public void save(User user);
}
Create Service implementation class:
public class UserServiceImpl implements UserService{
/ DI: Dependency Injection Dependency Injection. An IOC environment is required. In the process of creating this class, Spring Spring sets the dependent properties of the class into it. /
private UserDaoImpl userDao; //Provide the right to create the set method to Spring
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
@Override
public void save(User user) {
userDao.save(user);
}
}
Create test class:
@Test
public void run1(){
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext6.xml");
UserService userService = (UserService)context.getBean("userService");
User user = (User)context.getBean("user");
userService.save(user);
}
If you integrate Junit4 for testing:
@RunWith(SpringJUnit4ClassRunner.class )
@ContextConfiguration(“classpath:ApplicationContext6.xml”)
public class UserTest {
@Resource(name=”userService”)
private UserService userService;
@Resource(name=”user”)
private User user;
@Test
public void run1(){
userService.save(user);
}
Test the output.
Two ways of
SpringIoC injection 1. Set injection
Set injection can provide a constructor (must be written)
2. Construct injection
Provide a constructor (must be written)


Annotation of IoC

Still use the above 4 classes, but the Spring configuration file needs to be modified to introduce context constraints
write picture description here

Scan for annotations
write picture description here
write picture description here
write picture description here
write picture description here

Spring provides three derived annotations of @Component: (the function is currently the same)
* @Controller : WEB layer
* @Service : business layer
* @Repository : persistence layer

These three annotations are to make the purpose of the annotated class itself clear, and Spring will enhance it in subsequent versions
@Value : used to inject common types.
@Autowired : Autowired:
* Assembled by type by default.
* Injection by name:
* @Qualifier: Forces the use of name injection.
@Resource is equivalent to:
* @Autowired is used with @Qualifier.

Bean's scope annotation:
@Scope:
* singleton: singleton
* prototype: multiple instances

Bean life cycle configuration:
@PostConstruct : equivalent to init-method
@PreDestroy : equivalent to destroy-method
write picture description here

XML and annotations:
* XML: clear structure.
* Annotations: easy to develop. (Attribute injection.)

There is also an integrated development of XML and annotations in actual development:
* Beans have XML configuration. But the attributes used are injected using annotations.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326100496&siteId=291194637