The next day Spring Framework knowledge summary

A: The difference between DBCP and C3P0 connection pool

DBCP connection pool object is created by BasicDataSource, and C3P0 connection pool object is created by ComboPooledDataSource

The steps are the same:

// create a pool of connections! !
BasicDataSource dataSource = new BasicDataSource (); or:
ComboPooledDataSource the dataSource = new new ComboPooledDataSource

// set four dataSource configuration information passed in
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc: MySQL: //127.0.0.1: 3306 / = utf8 database name characterEncoding?";
String username = "database user name";
String password = "database password";
dataSource.setDriverClassName (driverClass);
dataSource.setUrl (url);
dataSource.setUsername (username);
dataSource.setPassword (password);

// Create DBUtils core classes QueryRunner dataSource objects to pass into the
QueryRunner queryRunner = new new QueryRunner (dataSource);
// execute data processing sql statement
String sql = "the SELECT * from the Account";
List <the Account> List = queryRunner .query (sql, BeanListHandler new new <> (Account.class));
IF (List list.size = null && (!)> 0) {
for (the Account Account: List) {
System.out.println (Account);
}
}

II: Notes

1. Note: If you want to scan must be annotated with notes in xml (add context constraints)
xmlns: context = "http://www.springframework.org/schema/context"
http://www.springframework.org/schema / context
http://www.springframework.org/schema/context/spring-context.xsd
<- scan constraint opening spring -!>
<context: Component Base-package-scan = "to scan package" />

<context: property-placeholder location = "classpath: properties profile" />
Property the override-: reading properties file
location = obtain both properties have a profile manner:
CLASSPATH:: Find properties file in the current path
classpath * : in addition to also look at the introduction of the current file to find the package maven jar advanced applications

2. @ Component: Create English meaning class object implement assembly
Example: @Component :( "accountservice") = <bean id = "accountService" class = "com.itheima.service.AccountServiceImpl"> </ bean>

In order to better distinguish 3.Spring @Component annotations three layer structure may also change a mode annotation
@Controller: the control layer web annotated classes
@Service: control service classes in the annotation layer
@Repository: Control in class annotation layer dao

4.Scope annotation: single embodiment, many cases of multi-mode default number of patients required a single case @Scope ( "prototype")

5.init-method executed before the constructor after execution method destory-method object is destroyed: the method must be used in the
future @PostConStruct construction execution
performed before the object is destroyed @PreDestroy

Three: dependency injection annotations (String into dependency injection and jdk dependency injection, is recommended to use String Dependency Injection)

1. @ Autowired English automatically mean wearing String offer
@Qualifier ( "associated id") do not write default is associated id lowercase class name written on the recommendations associated the above mentioned id
@Autowired and @Qualifier ( "associated id") in conjunction Implementation Notes injection method does not need to write setXXX

2. @ Resource (name = "") jdk provided

Four: junit test:

Context = new new the ClassPathXmlApplicationContext the ClassPathXmlApplicationContext ( "applicationConext.xml");
the AccountService AccountService = context.getBean ( "AccountService", AccountService.class);
each test should write the above code to solve this problem requires Step 1. 2 leader packet. add annotations, 3. read the configuration file

Package guide: Note that to guide and sping version is a version of the package
<dependency>
<the groupId> org.springframework </ the groupId>
<the artifactId> Spring-Test </ the artifactId>
<Version> 5.0.2.RELEASE </ Version>
</ dependency>

Extraction service, add comments: @RunWith (SpringJUnit4ClassRunner.class)

Reads the configuration file: @ContextConfiguration (locations = "classpath: xml configuration file")
@ContextConfiguration (locations = "CLASSPATH: Profile XML") is equivalent to ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ( "applicationConext.xml ");

Guess you like

Origin www.cnblogs.com/lgpliuguoping/p/11449040.html