Spring 有几种配置方式?

发布于 2023-01-16 14:11:48 字数 6921 浏览 110 评论 0

将 Spring 配置到应用开发中有以下三种方式:

  1. 基于 XML 的配置
  2. 基于注解的配置
  3. 基于 Java 的配置

基于 XML 的配置

在 Spring 框架中,依赖和服务需要在专门的配置文件来实现,我常用的 XML 格式的配置文件。这 些配置文件的格式通常用开头,然后一系列的 bean 定义和专门的应用配置 选项组成。

SpringXML 配置的主要目的时候是使所有的 Spring 组件都可以用 xml 文件的形式来进行配置。这 意味着不会出现其他的 Spring 配置类型(比如声明的方式或基于 Java Class 的配置方式)

Spring 的 XML 配置方式是使用被 Spring 命名空间的所支持的一系列的 XML 标签来实现的。 Spring 有以下主要的命名空间:context、beans、jdbc、tx、aop、mvc 和 aso。

<beans>
<!-- JSON Support -->
<bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonV iew"/>
<bean class="org.springframework.web.client.RestTemplate"/>
</beans>

基于注解的配置

Spring 在 2.5 版本以后开始支持用注解的方式来配置依赖注入。可以用注解的方式来替代 XML 方 式的 bean 描述,可以将 bean 描述转移到组件类的 内部,只需要在相关类上、方法上或者字段声 明上使用注解即可。注解注入将会被容器在 XML 注入之前被处理,所以后者会覆盖掉前者对于同一 个属性的处理结 果。

注解的初衷是简化 xml 配置的,因此不能单独存在,比如下面要通过开关打开。而且,有些配置不方便用注解解决,比如 jdbc 的连接池,仍需 xml 配置。

注解装配在 Spring 中是默认关闭的。所以需要在 Spring 文件中配置一下才能使用基于注解的装配 模式。如果你想要在你的应用程序中使用关于注解的方法的话,请参考如下的配置:

<beans>
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>

在 <context:annotation-config/> 标签配置完成以后,就可以用注解的方式在 Spring 中向属 性、方法和构造方法中自动装配变量。

下面是几种比较重要的注解类型,分为2类:

负责 bean 定义

在 bean 的上面使用 @Component 或其子类(@Repository、@Service、@Controller)来定义 bean

负责 Bean 的注入

  1. @Required:该注解应用于设值方法。
  2. @Autowired:该注解应用于有值设值方法、非设值方法、构造方法和变量。
  3. @Qualifier:该注解和@Autowired 注解搭配使用,用于消除特定 bean 自动装配的歧义。
  4. JSR-250 Annotations:Spring 支持基于 JSR-250 注解的以下注解,@Resource、 @PostConstruct 和 @PreDestroy。

注解的配置原则是:基本配置使用xml(如数据库的配置),业务配置使用注解。

举例:先由控制层(Controller)调用服务层(Service),再由服务层调用Dao层(数据访问层),使用注解如下:

控制层 UserController.java:

@Controller
public class UserController {
@Autowired // 自动装配
private UserService userService;
//保存用户
public String save(User user) {
userService.save(user);
return "保存成功";
}
}

服务层 Service UserService.java:

@Service
public interface UserService {
//保存用户
void save(User user);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void save(User user) {
userDao.save();
}
}

Dao 层 UserDao.java:

@Repository
public interface UserDao{
void save();
}
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void save() {
// 保存数据的操作
}
}

基于 Java 的配置

Spring 3.0 以后,提供了 Java 配置的能力,Spring 4.x 和 SpringBoot 都推荐使用 Java 配置。

基于 java 的配置,简单来说是通过提供新的注解标签,提供完全替代 xml 的解决方案,以后可以不用类似 aplication.xml 作为配置入口了。

xml = 注解 + javaConfig

首先要说明以下两个注解:

@Configuration,表示修饰的类可以作为 bean 的来源(通过注解来获取bean,等价于 aplication.xml 的作用,作为入口类)
@Bean,表示实例化一个 bean,等同于在 xml 里面添加一个 bean 被 @Bean 修饰的方法名就是该 bean 的 name

最简单的 @Configuration 声明类请参考下面的代码:

@Configuration     
public class AppConfig{
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}

对于上面的 @Beans 配置文件相同效果的 XML 配置文件如下:

<beans>         
<bean class="com.somnus.services.MyServiceImpl"/>
</beans>

上述配置方式的实例化方式如下:利用 AnnotationConfigApplicationContext 类进行实例化

public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}

要使用组件组建扫描,仅需用 @ComponentScan 进行注解即可:

@Configuration 
@ComponentScan(basePackages = "com.somnus")
public class AppConfig { ... }

在上面的例子中,com.acme 包首先会被扫到,然后再容器内查找被 @Component 声明的类,找 到后将这些类按照 Sring bean 定义进行注册。

如果你要在你的 web 应用开发中选用上述的配置的方式的话,需要用 AnnotationConfigWebApplicationContext 类来读 取配置文件,可以用来配置 Spring 的 Servlet 监听器 ContextLoaderListener 或者 Spring MVC 的 DispatcherServlet:

<web-app>
<!-- 配置 ContextLoaderListener 时用 AnnotationConfigWebApplicationContext 替代默认的 XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.howtodoinjava.AppConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listenerclass>org.springframework.web.context.ContextLoaderListener
</listener -class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servletclass>org.springframework.web.servlet.DispatcherServlet</servletclass>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.howtodoinjava.web.MvcConfig</paramvalue>
</init-param>
</servlet>
<!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

文章
评论
28 人气
更多

推荐作者

佚名

文章 0 评论 0

今天

文章 0 评论 0

゛时过境迁

文章 0 评论 0

达拉崩吧

文章 0 评论 0

呆萌少年

文章 0 评论 0

孤者何惧

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文