Spring takes notes the next day

Keywords: Spring JDBC MySQL SQL

Spring Learning Notes

1. Implementation of AOP based on aspect annotations

Step 1, Create Bean s for Objects

<!-- create object -->
<bean id="book" class="com.itcast.aop.Book"></bean>
<bean id="myBook" class="com.itcast.aop.MyBook"></bean>

Step 2: Open AOP operations in the spring core configuration file

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd 
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- open aop operation -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

Step 3. Using annotations on enhanced classes

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MyBook {

	//Enhanced configuration using annotations on Methods
	@Before(value="excution(* com.itcast.aop.Book.*(...))")
	public void before() {
		System.out.println("Before.....");
		
	}
}

Spring uses annotations to implement packages that AOP needs to import

2. Implement database operations using the JDBC template (JDBCTempelete) in Spring

First, the update() function in JDBCTempelete can be used to complete adding, deleting and modifying operations

For example:

public void testOne() {
		DriverManagerDataSource ds=new DriverManagerDataSource();
		ds.setDriverClassName("com.mysql.jdbc.Driver");
		ds.setUrl("jdbc:mysql://localhost:3306/draw_life");
		ds.setUsername("root");
		ds.setPassword("123456");
		
		JdbcTemplate jt=new JdbcTemplate(ds);
		
		//Modify Action
		String sql="update User set password=? where Name=?";
		int rows=jt.update(sql, "5555","tom");
		//Add Action
		String sql2="insert into User values(?,?)";
		int rows2=jt.update(sql2, "bok","5555");
		//Delete operation
		String sql3="delete from user where Name=?";
		int rows3=jt.update(sql3, "tom");
		
		System.out.println(rows);
		System.out.println(rows2);
		System.out.println(rows3);
	}

Second, Query

JdbcTempelete implements queries with an interface, RowMapper, for which jdbcTempelete does not provide an implementation class, so it needs to encapsulate its own data.

There are three types of jdbcTempelete completion queries:

(1) Query returns a value

public void testCount() {
		DriverManagerDataSource ds=new DriverManagerDataSource();
		ds.setDriverClassName("com.mysql.jdbc.Driver");
		ds.setUrl("jdbc:mysql://localhost:3306/draw_life");
		ds.setUsername("root");
		ds.setPassword("123456");
		
		JdbcTemplate jt=new JdbcTemplate(ds);
		
		String sql="select count(*) from user";
		
		//The first parameter is the sql statement, and the second parameter is
		int count=jt.queryForObject(sql, Integer.class);
		System.out.println(count);
}

(2) Query returns an object

public void testObject() {
		DriverManagerDataSource ds=new DriverManagerDataSource();
		ds.setDriverClassName("com.mysql.jdbc.Driver");
		ds.setUrl("jdbc:mysql://localhost:3306/draw_life");
		ds.setUsername("root");
		ds.setPassword("123456");
		
		JdbcTemplate jt=new JdbcTemplate(ds);
		
		String sql="select * from User where Name=?";
		User user=jt.queryForObject(sql, new MyRowMapper(), "tom");
		System.out.println(user);
	}
}
class MyRowMapper implements RowMapper<User>{

	@Override
	public User mapRow(ResultSet rs, int num) throws SQLException {
		//Get the returned result set
		String name=rs.getString("Name");
		String password=rs.getString("password");
		//Encapsulate results into User classes
		User user = new User();
		user.setName(name);
		user.setPassword(password);
		return user;
	}
}

(3) Query returns a List collection

public void testList() {
		DriverManagerDataSource ds=new DriverManagerDataSource();
		ds.setDriverClassName("com.mysql.jdbc.Driver");
		ds.setUrl("jdbc:mysql://localhost:3306/draw_life");
		ds.setUsername("root");
		ds.setPassword("123456");
		
		JdbcTemplate jt=new JdbcTemplate(ds);
		
		String sql="select * from user";
		
		//The third parameter args in jt.query(sql, rowMapper, args) can be omitted
		List<User> list=jt.query(sql, new MyRowMapper());
		System.out.println(list);
	}
}
class MyRowMapper implements RowMapper<User>{

	@Override
	public User mapRow(ResultSet rs, int num) throws SQLException {
		//Get the returned result set
		String name=rs.getString("Name");
		String password=rs.getString("password");
		//Encapsulate results into User classes
		User user = new User();
		user.setName(name);
		user.setPassword(password);
		return user;
	}
}

Third, use Spring to configure connection pools and DAO s using JDBCTempelete

(1) Importing jar packages

c3p0-0.92.1.jar and mchange-commons-java-0.2.3.4.jar

(2) Create a Spring profile to configure connection pools

<!-- To configure c3p0 Connection Pool -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- Inject property values inside -->
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/draw_life"></property>
    <property name="user" value="root"></property>
    <property name="password" value="123456"></property>
</bean>

3. Comparatively complete constraints

<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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>

4. Auto-binding for Annotation Version

First, from autowire to @Autowired

Default autowires can be used to specify the default autobinding method, or autowires on each bean can be used to specify the autobinding method for each bean.@Autowired is a core annotation based on annotation-dependent injection that can be labeled in multiple locations of a class definition, for example:

(1): Attribute (domain): Whether the declared access restrictors are private, protected, or public, their required dependency injection requirements can be met as long as @Autowired is labeled.

(2): Construction method definition: Equivalent to how a method "construct" binding is constructed in a bean, which determines what dependent objects are injected into the current object based on the type of parameters in the construction method.

(3): Method Definition: @Autowired can be labeled not only on traditional setter methods, but also on any nominal method definition as long as the method defines parameters that need to be injected.

Once the @Autowired label is defined in the class, you can make the entire application work by appending the AutowiredAnnotationBeanProcessor to the IOC container's configuration file, provided, of course, that we need to use an ApplicationContext-type container.As follows:

        

Second, @Qualifier usage

The @Autowired matches by type, which does not apply if the dependency of the current @Autowired label can be found on two or more instances of the same type of object in the container.This can be further qualified with @Qualifier.@Qualifier is an auto-binding annotation version of byName that can also be labeled on attributes (domains), construction methods, or method definitions.For example:

              

           

Third, JSR250's @Resource, @PostConstruct, and @PreDestroy

  1. @Resource is different from @Autowired in that it follows the byName autobinding behavior, which means that the IOC container will look for an example of a beanName in the container based on the name specified by @Resource, and then inject the found object instance into the object labeled by @Resource.@Resource can also be labeled on construction methods or common method definitions.
  2. The @PostConstruct and @PreDestroy are not intended for dependency injection; they are primarily used to label methods related to object life cycle management, similar to spring's InitializingBean and DisposableBean interfaces, as well as init-method and destroy-method in configuration items.As with @Autowired, after adding the JSR250 label, you need to add BeanPostProcessor to the IOC container to make the application run.All JSR250 needs is org.springframwork.context.annotation.CommonAnnotationBeanPostProcessor.The configuration is as follows:
  3. Whether @Autowired or @Resource requires BeanPostProcessor to be added to the container, so you can use a <context:annotation-config>configuration in your XSD-based configuration file to resolve all of the BeanPostProcessor configurations above.<context:annotation-config>Registers not only AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor in containers, but also persistenceAnnotationBeanPostProcessor and RequestAnnotationBeanPostProcessor in one go.

 

Four original articles were published, 3 were praised, and 45 were visited
Private letter follow

Posted by Smruthi on Mon, 17 Feb 2020 17:35:45 -0800