一、开发环境
1.idea 2018.1
2.maven 3.3.3
3.jdk1.8
二、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ygq</groupId>
<artifactId>spring_02_DI</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
</project>
三、spring核心配置文件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: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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启包扫描-->
<context:component-scan base-package="com.ygq"/>
<!--开启注解扫描-->
<context:annotation-config />
<!--由 Spring容器创建该类的实例对象 –>-->
<!--<bean id="messageDao" class="com.ygq.dao.MessageDao" />-->
<!--<bean id="messageService" class="com.ygq.service.impl.MessageServiceImpl" />-->
<!--<bean id="messageController" class="com.ygq.controller.MessageController" />-->
</beans>
四、项目结构
1.整体结构
2.MessageDao.java
package com.ygq.dao;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;
@Repository
public class MessageDao {
public String sayHello(){
return "Hello I'm MessageDao!";
}
}
3.MessageService.java
package com.ygq.service;
public interface MessageService {
public String sayHello();
}
4.MessageServiceImpl.java
package com.ygq.service.impl;
import com.ygq.dao.MessageDao;
import com.ygq.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
private MessageDao messageDao;
public String sayHello() {
String s = messageDao.sayHello();
System.out.println(s);
return "Hello I'm MessageServiceImpl!";
}
}
5.测试类 com.ygq.service.impl.MessageServiceImplTest.java
package com.ygq.service.impl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class MessageServiceImplTest {
@Autowired
private MessageServiceImpl messageServiceImpl;
@Test
public void sayHello() {
String s = messageServiceImpl.sayHello();
System.out.println(s);
}
}
6.运行结果
Hello I'm MessageDao!
Hello I'm MessageServiceImpl!