In most cases, using spring to configure Dependency injection is configured with annotations because annotations are easier and simpler than XML. Except for objects such as data source objects, where configuration information is easily modifiable, it is more appropriate to use an XML file for configuration, which makes it easier to modify externally without having to open the code.
The next step is to explain how the annotations are configured, first to let spring support annotations, and to edit the spring configuration file contents as follows:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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 "> <!-- 让spring支持注解 --> <context:annotation-config/> <!-- 指定哪些包下的类受可以让Spring通过注解来管理 --> <context:component-scan base-package="org.zero01"/></beans>
With the annotation configuration, let Spring help us create the object, the student class code is as follows:
package org.zero01;import org.springframework.stereotype.Component;// 加上这个注解表示该类受到Spring的管理,注解的值为该类的id,该注解的作用相当于xml中的bean标签@Component("stu")public class Student { ...
Test code:
package org.zero01;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); // 同样的通过配置的id获得实例对象 Student stu1 = (Student) app.getBean("stu"); Student stu2 = (Student) app.getBean("stu"); // 默认都是单例对象 if (stu1 == stu2) { System.out.println("单例对象"); }else{ System.out.println("非单例对象"); } }}
Operation Result:
单例对象
You can not configure the ID value when using annotations, write directly on the @Component line:
package org.zero01;import org.springframework.stereotype.Component;@Componentpublic class Student { ...
It then obtains the instance object through the class of the classes:
Student stu1 = app.getBean(Student.class);
But the flexibility of this approach is not as good as using the ID value, because the string can be changed by variable, and this method of using class is equivalent to writing dead code.
If you do not want to take out a singleton object from the container, you can use @Scope annotations to configure the use of prototype mode, and the value of the configuration property can be configured with @Value annotations, for example:
package org.zero01;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import org.springframework.context.annotation.Scope;@Component("stu")@Scope(value = "prototype") // 取值与xml中的scope属性是一样的public class Student { @Value("小明") private String name; @Value("15") private int age; @Value("南京") private String address; ...
Test code:
package org.zero01;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu1 = app.getBean(Student.class); Student stu2 = app.getBean(Student.class); if (stu1 == stu2) { System.out.println("单例对象"); }else{ System.out.println("非单例对象"); } System.out.println(stu1.getName()); System.out.println(stu1.getAge()); System.out.println(stu1.getAddress()); }}
Operation Result:
非单例对象小明15南京
Note: We can write @Value annotations on the setter method of the property, and write the same function on the property.
If you need to inject a self-built type, there are two annotations that can be @Resource and @Autowired, but to configure dependency injection through these two annotations, the injected object needs to write @Component annotations:
package org.zero01;import org.springframework.stereotype.Component;@Component("phone")public class Phone {}package org.zero01;import org.springframework.stereotype.Component;@Component("dog")public class Dog {}
You can then use @Resource and @Autowired annotations to configure Dependency injection, Student class code:
package org.zero01;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import org.springframework.context.annotation.Scope;import javax.annotation.Resource;@Component("stu")@Scope(value = "prototype")public class Student { @Value("小明") private String name; @Value("15") private int age; @Value("南京") private String address; @Resource private Dog dog; @Autowired private Phone phone; ...
Test code:
package org.zero01;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu1 = app.getBean(Student.class); System.out.println(stu1.getName()); System.out.println(stu1.getAge()); System.out.println(stu1.getAddress()); System.out.println(stu1.getDog()); System.out.println(stu1.getPhone()); }}
Operation Result:
小明15南京[email protected][email protected]
A brief description of the difference between @Autowired and @Resource:
- Purpose: Used as a bean injection
- History: @Autowired annotations that are spring, @Resource annotations that are not part of spring, are annotations supported by JDK1.6
- Common denominator: assemble beans. Write on the field, or write in setter method
- Different points:
- @Autowired By default assembly by type, dependent objects must exist, if you want to allow null values, you can set its Required property to False, for example:
@Autowired(required=false) , you can also use the name assembly, with @Qualifier annotations.
- @Resource is a JDK1.6 supported annotation, by default assembly by name, name can be specified by the Name property, if the Name property is not specified, when the annotation is written on the field, the default is the field name, by name, if the annotation is written on the setter method by default property name assembly. When a bean matching the name cannot be found, it is assembled by type. However, it is important to note that if the Name property is specified once, it will only be assembled by name.
- Convenience: Both are similar in convenience and can be automatically assembled
- Coupling problem: It may be said that the use of Java-brought @Resource can reduce the coupling with spring, but in fact the annotation processor we use is provided by spring, is the same, does not matter the decoupling of the argument
Use the annotations described above to do a simple and easy to find and change small examples:
The Pom.xml file is configured as follows:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency> </dependencies>
The spring configuration file reads as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" Xmlns:contex t= "Http://www.springframework.org/schema/context" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/conte XT Http://www.springframework.org/schema/context/spring-context.xsd "> <!--let spring support annotations--< ;context:annotation-config/> <!--specifies which packages under which classes are allowed to be managed by spring through annotations--<context:component-scan base-package= "O Rg.zero01 "/> <!--configuring data source objects--<bean id=" DataSource "class=" "Com.mchange.v2.c3p0.ComboPooledDataSource" p:driverclass= "Com.mysql.jdbc.Driver" p:jdbcurl= "Jdbc:mysql:///school" p:user= "root" p:p Assword= "Your_password" P:maXpoolsize= "Ten" p:minpoolsize= "1" p:logintimeout= "/></beans>"
The first is the interface code:
package org.zero01.dao;import org.zero01.pojo.Student;import java.util.List;public interface DAO { public int insert(Student student) throws Exception; public int delete(int sid) throws Exception; public List<Student> selectAll() throws Exception; public int update(Student student) throws Exception;}package org.zero01.service;import org.zero01.pojo.Student;import java.util.List;public interface Service { public int enterSchool(Student student); public int dropOut(int sid); public List<Student> getStudents(); public int updateData(Student student);}package org.zero01.view;import org.zero01.pojo.Student;import java.util.List;public interface View { public int enterSchool(Student student); public int dropOut(int sid); public List<Student> getStudents(); public int updateData(Student student);}
Then is the specific implementation class code, the Studentdao class:
Package Org.zero01.dao;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.component;import Org.zero01.pojo.student;import Javax.sql.DataSource;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Java.util.arraylist;import java.util.List; @Component ("Studao") public class Studentdao implements DAO {@Autowir Ed private DataSource DataSource; public int Insert (Student Student) throws Exception {Connection Connection = null; try {connection = datasource.getconnection (); String sql = "INSERT into student (sname,age,sex,address) VALUES (?,?,?,?)"; PreparedStatement preparedstatement = connection.preparestatement (sql); Preparedstatement.setstring (1, Student.getsname ()); Preparedstatement.setint (2, Student.getage ()); Preparedstatement.setstring (3, Student.getsex ()); PreparedstatEment.setstring (4, student.getaddress ()); int row = Preparedstatement.executeupdate (); if (Row > 0) {return row; }} catch (SQLException e) {e.printstacktrace (); } finally {connection.close (); } return 0; } public int Delete (int sid) throws Exception {Connection Connection = null; try {connection = datasource.getconnection (); String sql = "DELETE from student WHERE sid=?"; PreparedStatement preparedstatement = connection.preparestatement (sql); Preparedstatement.setint (1, SID); int row = Preparedstatement.executeupdate (); if (Row > 0) {return row; }} catch (SQLException e) {e.printstacktrace (); } finally {connection.close (); } return 0; } public list<student> SelectAll () throws Exception {Connection connection = null; try {connection = datasource.getconnection (); String sql = "SELECT * from student"; PreparedStatement preparedstatement = connection.preparestatement (sql); ResultSet ResultSet = Preparedstatement.executequery (); list<student> students = new arraylist<student> (); while (Resultset.next ()) {Student Student = new Student (); Student.setsid (Resultset.getint ("Sid")); Student.setsname (resultset.getstring ("sname")); Student.setsex (resultset.getstring ("Sex")); Student.setaddress (resultset.getstring ("Address")); Students.add (student); } return students; } catch (SQLException e) {e.printstacktrace (); } finally {connection.close (); } return null; } public int update (Student Student) throws Exception {Connection Connection = null; try {connection = datasource.getconnection (); String sql = "UPDATE student SET sname=?,age=?,sex=?,address=?" WHERE sid=? "; PreparedStatement preparedstatement = connection.preparestatement (sql); Preparedstatement.setstring (1, Student.getsname ()); Preparedstatement.setint (2, Student.getage ()); Preparedstatement.setstring (3, Student.getsex ()); Preparedstatement.setstring (4, student.getaddress ()); Preparedstatement.setint (5, Student.getsid ()); int row = Preparedstatement.executeupdate (); if (Row > 0) {return row; }} catch (SQLException e) {e.printstacktrace (); } finally {connection.close (); } return 0; }}
Schoolservice Code:
Package Org.zero01.service;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.component;import Org.zero01.dao.dao;import Org.zero01.pojo.student;import java.util.List; @Component ("Schoolservice") public class Schoolservice implements Service {@Autowired private dao DAO ; public int Enterschool (Student Student) {try {return Dao.insert (Student); } catch (Exception e) {e.printstacktrace (); } return 0; } public int dropout (int sid) {try {return dao.delete (SID); } catch (Exception e) {e.printstacktrace (); } return 0; } public list<student> getstudents () {try {return dao.selectall (); } catch (Exception e) {e.printstacktrace (); } return null; } public int UpdateData (Student Student) {try {return dao.update (Student); } catch (ExceptIon e) {e.printstacktrace (); } return 0; }}
Schoolaction Code:
package org.zero01.view;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.zero01.pojo.Student;import org.zero01.service.Service;import java.util.List;@Component("stuAction")public class SchoolAction implements View { @Autowired private Service schoolService; public int enterSchool(Student student) { return schoolService.enterSchool(student); } public int dropOut(int sid) { return schoolService.dropOut(sid); } public List<Student> getStudents() { return schoolService.getStudents(); } public int updateData(Student student) { return schoolService.updateData(student); }}
As can be seen from the above code, we did not write the code about any instantiation object in which class, but instead gave the instantiation of the work to the spring container to help us complete, so that each class does not need to manage, maintain its own dependent objects, only need to complete their own business code, This weakens the dependency between classes and classes, reduces the complexity of the code, and each class only needs to maintain its own business code, which is the benefit of Spring's IOC module. And each class relies on an interface, not a specific implementation class, which conforms to the dependency reversal principle, does not cause code to be tightly coupled, and does not affect other classes when the specific implementation class is replaced.
Test code:
Package Org.zero01;import Org.json.jsonobject;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.zero01.view.view;import Org.zero01.pojo.student;import Java.util.hashmap;import Java.util.map;public class Test {public static void main (Strin G[] args) {ApplicationContext app = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); View view = (view) app.getbean ("Stuaction"); Student Student = (Student) app.getbean ("Student"); System.out.println ("Enterschool () affects the number of lines:" + view.enterschool (student)); System.out.println ("Dropout () affects the number of rows:" + view.dropout (25)); map<string, object> map = new hashmap<string, object> (); Map.put ("Studentlist", view.getstudents ()); Map.put ("Length", view.getstudents (). Size ()); System.out.println (new Jsonobject (map)); Student.setsname ("Xiao Gang"); Student.setaddress ("Changsha"); student.seTage (18); Student.setsid (29); System.out.println ("UpdateData () affects the number of lines:" + view.updatedata (student)); }}
Operation Result:
enterSchool() 影响行数:1dropOut() 影响行数:1{ "studentList": [ { "address": "南京", "sname": "小明", "sex": "男", "age": 0, "sid": 26 }, { "address": "南京", "sname": "小明", "sex": "男", "age": 0, "sid": 27 }, { "address": "南京", "sname": "小明", "sex": "男", "age": 0, "sid": 28 }, { "address": "南京", "sname": "小明", "sex": "男", "age": 0, "sid": 29 }, { "address": "南京", "sname": "小明", "sex": "男", "age": 0, "sid": 30 }, { "address": "南京", "sname": "小明", "sex": "男", "age": 0, "sid": 31 } ], "length": 6}updateData() 影响行数:1
Spring uses annotations to configure dependency injection