使用普通的java工程或者web工程,需要下载Spring的相关jar包。
使用maven搭建,省去了下载jar包这一步。下面就使用maven来搭建一个Spring项目。
一、demo
1、demo1:
(1)pom添加相关依赖:
4.0.0
com.demo
mavenspring
0.0.1-SNAPSHOT
org.springframework
spring-core
4.3.13.RELEASE
org.springframework
spring-context
4.3.13.RELEASE
runtime
log4j
log4j
1.2.17
(2)在resources资源文件夹下创建两个文件:
log4j.properties为日志文件:
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.beans.factory=DEBUG
applicationContext.xml为Spring的配置文件:在配置文件中完成注入
ref表示注入对象,基本类型用value。
(3)业务代码:
public class User {
private int id;
private String name;
//省略set、get
}
public class UserDao {
public User getUserById(int i) {
User user = new User();
user.setId(10);
user.setName("zhangsan");
return user;
}
}
public class UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
测试类:现在就不需要像以前一样去new对象了:
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//把IOC容器中的us对象取出来
UserService service = context.getBean("us", UserService.class);
User user = service.getUserDao().getUserById(10);
System.out.println(user.getName());
}
}
2、demo2:上面用的是配置文件的方法去注入容器,还有一种是注解的方法(具体见三讲解):下面结合Spring整合hibernate来写一个实例:
4.0.0
com.demo
mavenspringhibernate
0.0.1-SNAPSHOT
org.springframework
spring-core
4.3.13.RELEASE
org.springframework
spring-context
4.3.13.RELEASE
runtime
org.springframework
spring-orm
4.3.13.RELEASE
org.springframework
spring-jdbc
4.3.13.RELEASE
org.hibernate
hibernate-core
4.3.11.Final
log4j
log4j
1.2.17
其中applicationContext.xml为总的配置文件:
org.hibernate.dialect.Oracle10gDialect
create
true
true
import org.springframework.stereotype.Component;
//把user对象创建(默认的名字为:对象的名字小写),然后加入到容器中
@Entity
@Table(name="t_user")
@Component
public class User {
private int id;
private String name;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Repository
public class UserDao {
@Resource
private SessionFactory sessionFactory;
@Resource
private User user;
public void save() {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
user.setId(1002);
user.setName("测试");
session.save(user);
tx.commit();
session.close();
}
}
@Service
public class UserService {
@Resource
private UserDao userDao;
public UserService() {
}
public UserService(UserDao userDao) {
this.userDao = userDao;
System.out.println("userservice construct run...");
}
}
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao bean = context.getBean(UserDao.class);
bean.save();
}
}
3、demo3:
(1)pom:
4.0.0
com.demo
mymvc
0.0.1-SNAPSHOT
war
4.3.13.RELEASE
org.springframework
spring-core
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.aspectj
aspectjrt
1.8.10
aopalliance
aopalliance
1.0
org.aspectj
aspectjweaver
1.8.10
org.hibernate
hibernate-core
4.3.11.Final
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet
jstl
1.1.2
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
jstl
jstl
1.2
taglibs
standard
1.1.2
log4j
log4j
1.2.17
com.alibaba
fastjson
1.2.38
com.fasterxml.jackson.core
jackson-core
2.9.2
com.fasterxml.jackson.core
jackson-databind
2.9.2
(2)spring.xml为spring配置文件:
(3)springmvc.xml为springmvc的配置文件:
(4)web.xml中引入上述两个配置文件,让tomcat在启动的时候扫描这两个文件:
contextConfigLocation
classpath:spring.xml
default
*.js
default
*.css
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcher
/
org.springframework.web.context.ContextLoaderListener
@Controller
public class HomeController {
@RequestMapping("/home")
public String goHome() {
System.out.println("hello");
return "home";
}
@RequestMapping("/home1")
@ResponseBody
public List goHome1() {
List list=new ArrayList<>();
Person person=new Person();
person.setId(1);
person.setName("zs");
list.add(person);
person=new Person();
person.setId(2);
person.setName("李四");
list.add(person);
System.out.println("hello1");
return list;
}
}
前台:home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
hello