I. Configuring a non-custom bean (data source datasource model)
dbcp Data Source:
Import DBCP jar package: Dbcp+pool+connector
Code implementation:
//Create a Data source object
Basicdatasource dataSource = new Basicdatasource ();
//Set basic parameters of the database
datasource.setdriverclassname ("Com.mysql.jdbc.Driver");
Datasource.seturl ("Jdbc:mysql:///test");
datasource.setusername ("* * * *");
Datasource.setpassword ("* * * *");
//Get connection resources from the data source
Connection Connection = datasource.getconnection ();
//JDBC Operation
System.out.println (connection);
c3p0 Data Source:
Import c3p0 jar package: C3p0+connector
Code implementation:
//Create a Data source object
Combopooleddatasource dataSource = new Combopooleddatasource ();
//Set basic parameters of the database
Datasource.setdriverclass ("Com.mysql.jdbc.Driver");
Datasource.setjdbcurl ("Jdbc:mysql:///test");
Datasource.setuser ("* * * *");
Datasource.setpassword ("* * * *");
//Get connection resources from the data source
Connection Connection = datasource.getconnection ();
//JDBC Operation
System.out.println (connection);
to configure a data source object in spring:
<!--The context namespace is required to load the Jdbc.properties file into a container in spring
classpath: The Class Load path development environment is SRC
-
<context:property-placeholder location= "classpath:jdbc.properties"/>
<!--configuring C3P0 data Sources --
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name= "Driverclass" value= "${jdbc.driver}" ></property>
<property name= "Jdbcurl" value= "${jdbc.url}" ></property>
<property name= "user" value= "${jdbc.username}" ></property>
<property name= "password" value= "${jdbc.password}" ></property>
</bean>
Second, spring's annotation development
What are the pros and cons of annotations and XML configuration files?
XML Benefits: Decoupling drawbacks: Cumbersome configuration
Note Advantages: Quick drawbacks: Coupling
1. Spring Original Annotations
The main purpose of the appearance is to replace the XML configuration of the custom bean
1.1 Development steps:
1. Import additional Jar:spring-aop.jar
2. Turn on component scanning in XML
<!--component Scan: tells the spring container which packets under which the bean needs to be scanned-
<context:component-scan base-package= "Com.cyxz" ></context:component-scan>
3. Development with annotations on entity beans
@Component ("Customerdao")
Public class Customerdaoimpl implements Customerdao
@Component ("CustomerService")
Public class Customerserviceimpl implements CustomerService
@Autowired
private Customerdao Customerdao;
4. Testing
1.2 Explanation of the annotations
IoC: Creating Objects
@Component (The Bean object's identity/id): On the bean that needs to be instantiated by spring, the argument string can not be written, the default is the first letter of the current class name lowercase
@Controller: The controller uses a web-tier bean, such as an action
@Service: Services that use service tier beans such as Customerserviceimpl
@Repository: The warehouse uses the DAO layer on the bean such as Customerdaoimpl
DI: Dependency Injection, annotation injection can omit set method
@Autowired: Automatically injected by the type of entity
@Qualifier ("Customerdao")//here @qualifier is matched by name but is used here @qualifier combined with @autowired
@Resource (name= "Customerdao")//@[email Protected][email protected]
@Value (value)
Other:
@Scope: Configure the Bean's scope, value singleton (default), and prototype
@PostConstruct: Specifies the initialization method//executes after construction
Predestroy: Specify the Destruction method//execute before destroying
Note: @Controller, @Service, @Repository, @Autowired, @Scope
Note: Development guidelines: Beans are custom then use the XML configuration when using annotation configuration, beans are non- customizable
2. Spring New annotation (secondary focus)
Spring's new annotations appear to completely replace the XML configuration file
To replace an XML file with a configuration class
using annotations instead of labels
@Configuration//Labeling the class is a spring configuration class
@ComponentScan ("COM.CYXZ")//Component Scan
@Import ({datasourceconfiguration.class})//Introduce additional configuration classes
@PropertySource ({"Classpath:jdbc.properties"})//load Jdbc.properties file
@Value ("${jdbc.driver}")//Match key in spring container
@Bean (name= "DataSource")//The return value of the method is stored in the spring container with the specified name
Third, spring integrated junit
the original test class:
@Test
//Test Get Service instance object from spring
Public void Test1 () throws exception{
ApplicationContext app = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
customerservice customerservice = (customerservice) app.getbean ("CustomerService");
Customerservice.save ();
}
Spring Integrated JUnit:
Development steps:
1. Import additional jars
Spring-test.jar
Spring-aop.jar
Junit.jar
2. Specify the test class and configuration file in the form of annotations
execution Test class: @RunWith (Springjunit4classrunner.class)
Specify configuration file (Class): @ContextConfiguration ("Classpath:applicationContext.xml")
3. Test which object in the spring container is injected through the annotations which object
4. Writing test Methods
Code implementation:
@RunWith (Springjunit4classrunner.class)
//@ContextConfiguration ("Classpath:applicationContext.xml")//Load configuration file
@ContextConfiguration (Classes={springconfiguration.class})//Load Configuration class
Public class Springjunittest {
@Autowired
private Customerdao Customerdao;
@Autowired
private customerservice customerservice;
@Test
Public void Test1 () {
Customerdao.save ();
}
@Test
Public void Test2 () {
Customerservice.save ();
}
}
Summary of Spring knowledge points (Ii.)