spring
1.spring容器
@component注解解释
使用该注解标记的类会被spring自动创建对象,不用使用new关键字
@componentScan
使用该注解标记的类会搜索有component注解的类
applicationcontext 是spring的容器 使用它的实现类AnnotationConfigApplicationContext用于向容器中添加数据
实例代码
package hello;
import org.springframework.stereotype.Component;
/**
* @author GuoZhaoning
*/
@Component
public class MessageService {
public MessageService() {
super();
System.out.println("MessageService...");
}
/**
*
* 消息服务
* @return
*/
public String getMessage(){
return "hello world";
}
}
package hello;
import org.springframework.stereotype.Component;
/**
* @author GuoZhaoning
*/
@Component
public class MessagePrinter {
public MessagePrinter() {
super();
System.out.println("MessagePrinter...");
}
private MessageService messageService;
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
public void printMessage() {
System.out.println(messageService.getMessage());
}
}
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author GuoZhaoning
*/
@ComponentScan
public class ApplicationSpring {
public static void main(String[] args) {
System.out.println("ApplicationSpring");
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationSpring.class);
}
}
2.获取容器中的对象
使用getbean方法
实例代码
MessageService messageService = applicationContext.getBean(MessageService.class);
MessagePrinter messagePrinter = applicationContext.getBean(MessagePrinter.class);
System.out.println(messageService);
System.out.println(messagePrinter);
3.管理对象之间的关联关系
使用@autowired注解,自动调用注解下的方法
实例代码
private MessageService messageService;
@Autowired
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
MessagePrinter messagePrinter = applicationContext.getBean(MessagePrinter.class);
// MessageService messageService = applicationContext.getBean(MessageService.class);
// System.out.println(messageService);
// System.out.println(messagePrinter);
// messagePrinter.setMessageService(messageService);
messagePrinter.printMessage();
4.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="service" class="ServiceMessage"></bean>
<bean id="printer" class="ServicePrinter">
<property name="serviceMessage" ref="service"></property>
</bean>
</beans>
public class ApplicationSpringXml {
public static void main(String[] args) {
System.out.println("applicationXml");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过id获取,对应xml文件中的id值
ServicePrinter printer = (ServicePrinter) context.getBean("printer");
//通过class获取
ServicePrinter printer1 = context.getBean(ServicePrinter.class);
printer.getMessage();
}
}
5.加入log4j日志文件
加入依赖
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
加入log4j配置文件
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p - %m%n
log4j.category.org.springframework.beans.factory=DEBUG
6.idea的config配置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ViYs4yle-1593879079741)(C:\Users\GuoZhaoning\AppData\Roaming\Typora\typora-user-images\image-20200522203118839.png)]
关联作用,能给出更多的提示
7. 装配bean的三种方式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0cpj29GY-1593879079746)(C:\Users\GuoZhaoning\AppData\Roaming\Typora\typora-user-images\image-20200522203402830.png)]
自动装配就是三个注解的使用component,autowired,componentScan,以及ApplicationContext context = new AnnotationConfigApplicationContext(App.class);
有时候没有主类,没有地方放componentScan注解,需要进行解耦,方法单独创建一个lei,进行组件的扫描及配置。
配置类
@Configuration @ComponentScan public class ApplConfig { }
主类修改
public class App { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(ApplConfig.class); CDPlayer player = context.getBean(CDPlayer.class); player.player(); } }
注解
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MfH3DXyI-1593879079749)(C:\Users\GuoZhaoning\AppData\Roaming\Typora\typora-user-images\image-20200522210153418.png)]
8.加入junit单元测试
配置依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
正常测试
9.使用spring单元测试
配置依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
测试类
@RunWith(SpringJUnit4ClassRunner.class)//配置上下文环境
@ContextConfiguration(classes = ApplConfig.class)//配置需要测试的配置类
public class AppTest {
@Autowired//进行注入,相当于set方法
private CDPlayer player;
@Test
public void testPlay(){
player.player();
}
}
10.autowired注解的使用场景
1.构造函数可以使用
@Autowired public CDPlayer(CompactDisc compactDisc, Power power) { this.compactDisc = compactDisc; this.power = power; System.out.println("CDPlayer的多参数构造函数"); }