目录
- 一、说明
- 二、配置方式
- 三、配置示例
- 3.1 基于xml配置文件
- 3.2 基于注解的配置
- 3.3 基于java的配置
一、说明
二、配置方式
- 1.基于xml配置文件,spring诞生的一种配置方式
- 2.基于注解的配置,spring2.5+开始
- 3. 基于java的配置,javaConfig,spring3.0+开始
三、配置示例
3.1 基于xml配置文件
- 1.文件名为spring.xml,文件位置放入resources中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userService" class="com.learning.beanconfig.UserService"></bean>
</beans>
package com.learning.beanconfig;
/**
* @Author wangyouhui
* @Description 服务类
**/
public class UserService {
public void print(String hello) {
System.out.println(hello);
}
}
package com.learning.beanconfig;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author wangyouhui
* @Description 应用类
**/
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = (UserService)classPathXmlApplicationContext.getBean("userService");
userService.print("hello");
}
}
3.2 基于注解的配置
- 1.文件名为spring-annotation.xml,文件位置放入resources中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 设置自动扫描的包 -->
<!-- base-package:设置一个基础包,Spring会自动扫描该包及其子包下的组件 -->
<context:component-scan base-package="com.learning.beanannotation">
</context:component-scan>
</beans>
package com.learning.beanannotation;
import org.springframework.stereotype.Component;
/**
* @Author wangyouhui
* @Description 服务类
**/
@Component
public class UserService {
public void print(String hello) {
System.out.println(hello);
}
}
package com.learning.beanannotation;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author wangyouhui
* @Description 应用类
**/
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-annotation.xml");
UserService userService = (UserService)classPathXmlApplicationContext.getBean("userService");
userService.print("hello");
}
}
3.3 基于java的配置
- 1.这里是@Configuration注解,@Bean @Import等暂不介绍
package com.learning.beanjavaconfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration//作为配置类,代替xml文件
@ComponentScan(basePackages = {"com.learning.beanjavaconfig"})
public class SpringConfig {
}
package com.learning.beanjavaconfig;
import org.springframework.stereotype.Component;
/**
* @Author wangyouhui
* @Description 服务类
**/
@Component
public class UserService {
public void print(String hello) {
System.out.println(hello);
}
}
package com.learning.beanjavaconfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author wangyouhui
* @Description 应用类
**/
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = (UserService)annotationConfigApplicationContext.getBean("userService");
userService.print("hello");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)