spring学习笔记之---bean管理的注解方式
bean管理的注解方式
(一)使用注解定义bean
(1)常用注解
(2)实例
1.在pom.xml中进行配置
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies>
2.创建一个类,在类中写一个方法,在类上加一个注解@Component
package service; import org.junit.Test; import org.springframework.stereotype.Component; import sun.misc.Contended; @Component("UserService") public class UserService { public String Hello(){ return "hello"; } }
3.创建一个applicationContext.xml,在里面配置包扫描
<?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"> <!--开启注解扫描=======================--> <context:component-scan base-package="service"></context:component-scan> </beans>
4.创建一个log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
5.创建一个测试类
package test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.UserService; public class UserTest { @Test public void hellotest(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService=(UserService)applicationContext.getBean("UserService"); String s=userService.Hello(); System.out.println(s); } }
(二)属性注入的注解
(1)常用注解
1.属性注入
package service; import org.junit.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import sun.misc.Contended; @Component("UserService") public class UserService { @Value("小欢") private String name; public String Hello(){ return "hello"+name; } }
2.类注入
(a)UserService.java
package service; import dao.UserDao; import org.junit.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import sun.misc.Contended; import javax.annotation.Resource; @Component("UserService") public class UserService { @Value("小欢") private String name; @Resource(name="UserDao") private UserDao userDao; public String Hello(){ System.out.println("service中的hello"); return "hello"+name; } }
(b)UserDao.java
package dao; import org.springframework.stereotype.Repository; @Repository("UserDao") public class UserDao { public void Hello(){ System.out.println("dao 中的hello"); } }
(c)applicationContext.xml
<?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"> <!--开启注解扫描=======================--> <context:component-scan base-package="service,dao"></context:component-scan> </beans>
(d)UserTest.java
package test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.UserService; public class UserTest { @Test public void hellotest(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService=(UserService)applicationContext.getBean("UserService"); String s=userService.Hello(); System.out.println(s); } }
(三)其他注解
(四)xml和注解整合开发
1.UserService2.java
package service; import dao.UserDao2; import org.springframework.stereotype.Repository; import javax.annotation.Resource; public class UserService2 { @Resource(name="UserDao2") private UserDao2 userDao2; public void He(){ userDao2.He(); System.out.println("userservice2中的he"); } }
2.UserDao2.java
package dao; public class UserDao2 { public void He(){ System.out.println("userdao2中的he"); } }
3.applicationContext.xml
<?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"> <context:annotation-config/> <bean id="UserService2" class="service.UserService2"/> <bean id="UserDao2" class="dao.UserDao2"/> </beans>
4.UserTest.java
package test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.UserService; import service.UserService2; public class UserTest { @Test public void hetest(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService2 userService2=(UserService2)applicationContext.getBean("UserService2"); userService2.He(); } }