The basic knowledge points are as follows:
- Introduce annotation constraints and configure component scanning
- Annotation on class: @ response @ Controller @Service @Repository
- Annotation @ value of common attribute
- Annotation @ Resource @ Autowired @ Qualifier for object properties
- Bean life cycle, initialization and destruction: @ PostConstruct @ PreDestroy
- Bean scope: @ Scope("prototype"). The default is singleton
1. Create a web project to introduce jar package
In addition to the basic six packages of IOC, the package of AOP development is introduced
2. Introduce the configuration file of spring
- Create applicationContext3.xml
- Introducing constraints: using annotation development to introduce context constraints
- file:///D:/Hibernate/Spring/spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.htm
<!-- ========================Introduce spring About annotation development context constraint======================== --> <?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: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"> </beans>
3. Create interface and implementation classes and test classes
package spring.day2.demo1; public interface UserDao { public void save(); }
package spring.day2.demo1; public class UserDaoImp1 implements UserDao { @Override public void save() { System.out.println("userdao Of save Method executed......."); } }
package spring.day2.demo1; import org.junit.Test; /* * IOC Introduction to annotation development */ public class SpringDemo1 { @Test /* * Traditional way */ public void demo1() { UserDao userDao = new UserDaoImp1(); userDao.save(); } }
4. Configure IOC component scanning in the configuration file to confirm which classes under which packages need annotations
5. Add notes
6. Add the following code to the test class
@Test /* * IOC Annotation method of */ public void demo2() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml"); UserDao bean = (UserDao) applicationContext.getBean("userDao"); bean.save(); }