Spring-----入门

时间:2025-01-01 11:35:14

Spring 入门

一、首先导入核心jar包

commons-logging-1.1.3.jar                 日志
        spring-beans-4.2.4.RELEASE.jar           bean节点
        spring-context-4.2.4.RELEASE.jar        spring上下文节点
        spring-core-4.2.4.RELEASE.jar            spring核心功能
        spring-expression-4.2.4.RELEASE.jar   spring表达式相关表

二、核心配置文件

Spring配置文件:applicationContext.xml   一般都写这个文件名

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <!--IOC: inverse of Control 控制反转:把创建对象的权利给spring容器
class:指明需要创建对象的类名和包名
spring通过反射来创建对象,默认使用newInstance()来创建对象
所以需要类中必须定义空参构造方法
--> <bean name="userService" class="com.luo.ioc.UserServiceImpl"></bean> </beans>

创建一个对象类接口及实现它的类

package com.luo.ioc;

public interface UserService {

    void addUser();
} package com.luo.ioc; public class UserServiceImpl implements UserService { @Override
public void addUser() {
System.out.println("Spring ioc 控制反转我来啦");
}
}

测试类:

package com.luo.test;

import com.luo.ioc.UserService;
import com.luo.ioc.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test_ioc { @Test
public void test1(){
//测试ioc 控制反转
String path="applicationContext_ioc.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
UserService userService = (UserService) applicationContext.getBean("userService");
userService.addUser();
}
}

Spring中的API:

        applicationContext:在加载spring配置文件时候创建对象

        ClassPathXmlApplicationContext:加载classPath下的spring配置文件(正常人都用这个)

        FileSystemXmlApplicationContext:根据文件的路径加载,不要求必须在项目的src下

        BeanFactory:在加载配置文件的时候不创建对象  (基本不用了)

 

测试类:

测试 ClassPathXmlApplicationContext  和FileSystemXmlApplicationContext

@Test
public void test1(){
//测试ioc 控制反转
String path="applicationContext_ioc.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
UserService userService = (UserService) applicationContext.getBean("userService");
userService.addUser();
} @Test
public void test2()
{ //C:\Users\qq\IdeaProjects\bean0.xml //spring的配置文件没有放在类路径下
ApplicationContext context=new FileSystemXmlApplicationContext("bean0.xml"); UserService userService=(UserService) context.getBean("userId"); userService.addUser();
} 测试BeanFactory
@Test
public void test1(){
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext_api.xml"));
UserService userService =(UserService) factory.getBean("userService");
userService.addUser();
}

用到的接口和类的代码如下:就写个依赖注入的  其他的都一样的   就不写了

//Dao层接口

package com.luo.di;

public interface UserDao {

    void  addUser();
} //Dao层实现类 package com.luo.di; public class UserDaoImpl implements UserDao{
@Override
public void addUser() {
System.out.println("spring di 依赖注入 我来啦");
}
} //Service接口
package com.luo.di; public interface UserService { void addUser();
} //ServiceImpl接口实现Service接口 package com.luo.di; public class UserServiceImpl implements UserService { //需要UserDao
private UserDao userDao; public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} @Override
public void addUser() { userDao.addUser();
}
}

依赖注入的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-4.2.xsd"> <!--依赖注入--> <bean name="udImpl" class="com.luo.di.UserDaoImpl"></bean>
<bean name="usImpl" class="com.luo.di.UserServiceImpl">
<property name="userDao" ref="udImpl"></property>
</bean>
</beans>

ref  : 引用