class=”markdown_views prism-atom-one-dark”>
Spring Learning (2)
Eight, use java to configure spring
The xml configuration of spring is not used at all, and it is all handed over to java
JavaConfig is a sub-project of spring, and it has become a core function after spring4!
//This will also be hosted by the Spring container and registered in the container, because it is originally a @Component
//Configuration means that this is a configuration class, just like the beans.xml we saw before
@Configuration
@ComponentScan("com.LH.pojo" )
@Import(Myconfig2.class)
public class Myconfig {
//Register a bean. It is equivalent to a bean tag 1 we wrote before
//The name of this method is equivalent to the id country in the bean tag
//The return value of this method is equivalent to the cLass attribute in the bean tag
@Bean
public User getUser(){
return new User ();// is to return The object injected into the bean
}
}
//Indicates that this class has been taken over by spring
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("pig")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';}
}
public class MyTest {
public static void main(String[ ] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Myconfig .class);
User getUser = (User) context.getBean ("getUser");
System.out.println(getUser.getName());
}
}
9. Proxy mode
Why learn the proxy mode? This is the bottom layer of SpringAOP (SpringAOP and SpringMVC)
Proxy mode:
- Static proxy
- Dynamic proxy
9.1, static proxy
Character Analysis:
- Abstract role: generally use interface or abstract class to solve
- Real role: Proxied role
- Proxy role: Proxy a real role, after proxying a real role, we generally do some subsidiary operations
- Client: the object to access the proxy
Code steps:
1. Interface
public interface Rent {
void rent();
}
2. Real characters
public class Host implements Rent{
public void rent(){
System.out.println("The landlord wants to rent out the house");
}
}
3. Proxy role
public class Proxy {
private Host host;
public Proxy() {
}
public Proxy(Host host) {
this.host = host;
}
public void rent(){
host.rent();
SeeHose();
hetong();
}
public void SeeHose(){
System.out.println("The intermediary will show you the house");
}
public void hetong(){
System.out.println("sign a contract") ;
}
}
4. Client Access Proxy
public class Client {
public static void main(String[ ] args) {
Host host =new Host();
//Agent, the agent role generally has subsidiary operations
Proxy proxy = new Proxy(host);
//You don't need to find a landlord, just find an intermediary to rent a house
proxy.rent();
}
}
Benefits of proxy mode:
- It can make the operation of real characters more pure, without having to care about some public business
- Public is also handed over to proxy characters! Realize business division of labor
- It is more convenient to expand public services and facilitate centralized management
Disadvantages:
- A real character will generate a proxy character, and the amount of code will double.�Do it as a business; it either works or it doesn’t
- Transactions are very important in project development. It involves data consistency, so don’t be careless!
- Ensuring completeness and consistency
The ACID principle of things:
- Atomicity
- Consistency
- Isolation
- Multiple businesses may operate the same resource to prevent data damage
- Persistence
- Once the transaction is committed, no matter what happens to the system, the result will not be affected, and it will be persistently written to the storage!
2. Spring transaction management
- Declarative transaction: aop
- Administrative affairs
<import resource="spring-dao.xml"/>
bean id="selectUser" class="com.LH.mapper.UserMapperImpl"
property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean
bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
constructor-arg name="dataSource" ref="dataSource" />
</bean
tx:advice span> id="txAdvice" transaction-manager="transactionManager"
tx:attributes span>
<!-- -->
<!-- -->
<!-- -->
tx:method name="*"/>
</tx:attributes
</tx:advice
aop:config
aop:pointcut span> id="txPointCut" expression="execution(* com.LH.mapper.*.*( ..))"/>
aop:advisor span> advice-ref="txAdvice" pointcut-ref="txPointCut"
</aop:config
Why do we need transactions?
- If you do not configure transactions, there may be cases where data submission is consistent!
- If we don’t configure declarative transactions in SPRING, we need to manually configure transactions in code!
utes>
Why do you need transactions?
- If you do not configure transactions, there may be data submission consistency!
- If we don't configure declarative transactions in SPRING, we need to manually configure transactions in code!
- Transactions are very important in the development of the project, which involves data consistency and integrity issues, and should not be sloppy!
tes
<!– –>
<!– –>
<!– –>
tx:method name=“*“/>
</tx:attributes
</tx:advice
aop:config
aop:pointcut span> id=“txPointCut“ expression=“execution(* com.LH.mapper.*.*( ..))“/>
aop:advisor span> advice-ref=“txAdvice“ pointcut-ref=“txPointCut“
</aop:config
Why do we need transactions?
- If you do not configure transactions, there may be cases where data submission is consistent!
- If we don’t configure declarative transactions in SPRING, we need to manually configure transactions in code!
utes>
Why do you need transactions?
- If you do not configure transactions, there may be data submission consistency!
- If we don't configure declarative transactions in SPRING, we need to manually configure transactions in code!
- Transactions are very important in the development of the project, which involves data consistency and integrity issues, and should not be sloppy!