《Spring实战》摘录 - 7

in #ievolution6 years ago

19-02-20-c0.jpg

61

Q: 在Web应用的web.xml文件中设置默认的profile
A:

<?xmlversion="1.0"encoding="utf-8"?>
<web-app version=2.5
    xmins="http://java.suncom/xmi/ns/javaee
    xmins:xsi=http://www.w3.org/2001/xmlschema-instance
    xsischemalocation="http://java.suncom/xml/ns/javaee
        http://java.suncom/xml/ns/javaee/web-app_2-5.xsd">
    <context-param>
        <param-name>contextconfiglocation</param-name>
        <param-value>/web-inf/spring/root-contextxml</param-value></context-param>
    </context-param>
    
    <context-param>
        <param-name>spring.profiles.default</param-name>  <------- 为上下文设置默认的profile
        <param-value>dev</param-value>
    </context-param>
    
    <listener>
        <1istener-class>
            org.springframeworkwebcontext.Contextloaderlistener
        </1istener-class>
    </listener>
    <servlet>
        <servlet-name>appservlet</servlet-name>
        <servlet-class>
            org.springframework.web.servletDispatche
        </servlet-class>
        <init-param>
            <param-name>spring.profiles.default</param-hame>   <------- 为Servlet设置默认的profile
            <param-value>dev</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>appservlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

62

Q: 重复使用元素来指定多个profile
A:

<?xml version="1.0" encoding="utf-8"?>
<beans xmins="http://www.springframework.org/schema/beans"
    xmins:xsi=http://www.w3.org/2001/xmlschema-instance"
    xmins:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee=http://www.springframework.org/schema/jee"
    xmins:p="http://www.springframework.org/schema/p"
    xsi:schemalocation="
        http://www.springframeworkorg/schema/jee
        http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-dbc.xsd
        http://www.springframeworkorg/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        
    <beans profile="dev">                     <------- dev profile的bean   
        <jdbc:embedded-database id="datasource">
            <jdbc:scriptlocation="classpath:schema.sql"/>
            <jdbc:scriptlocation="classpath:test-data.sql"/>
        </jdbc:embedded-database>
    </beans>
    
    <beans profile="qa">                     <------- qa profile的bean
        <bean id="datasource"
            class="org.apache.commons.dbcp.Basicdatasource"
            destroy-method="close"
            p:url="jdbc:h2:tcp://dbserver/~/test"
            p:driverClassName="org.h2.Driver"
            p:username="sa"
            p:password="password"
            p:initialsize="20"
            p:maxactive="30"/>
    </bean>
    
    <bean profile="prod">                     <------- prod profile的bean
        <jee:jndi-lookup id="dataSource"
                        jndi-name="jdbc/myDatabase"
                        resourcce-ref="true"
                        proxy-interface="javax.sql.DataSource" />
    </bean>
<beans>

63

Q: @Profile注解基于激活的profile实现bean的装配
A:

package com.myapp; 
import javax.sql.DataS0urce; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.profile; 
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBui1der; 
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 
import org.springframework.jndi.JndiObjectFactoryBean;

@configuration 
public class DataSourceConfig { 

    @Bean(destroyMethod="shutdown") 
    @Profi1e("dev")                      <------- 为 dev profile 装配的bean
    public DataSource embeddedDataSource ) ( 
        return new EmbeddedDatabaseBui1der() 
            .setType(EmbeddedDatabaseType.H2) 
            .addScript("classpath:schema.sg1") 
            .addScript("classpath:test—data.sg1") 
            .build();
    }
    
    @Bean 
    @profile("prod")                       <------- 为 prod profile 装配的bean
    public DataSource() { 
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName("jdbc/myDS"); 
        jndiObjectFactoryBean.setResourceRef(true); 
        jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource. class); 
        return (DataSource) jndiObjectFactoryBean.getObject(); 
    }
}

64

Q: 在Spring中装配bean的三种主要方式
A: 自动化配置、基于Java的显式配置以及基于XML的显式配置

65

Q: 我们该如何让Spring同时加载它和其他基于Java的配置呢?
A: 答案是@ImportResource注解

66

Q: p-命名空间属性是如何组成的。
A:

            属性名         所注入的bean的ID
          +---------+     +-----------+
          +         +     +           +
        P:compactDisc-ref="compactDisc"
        ^            |  |
        |            |  |
        |            |  |
        +            +--+
p-命名空间前缀    注入bean引用

67

Q: 选择构造器注入还是属性注入时的通用规则
A: 倾向于对强依赖使用构造器注入,而对可选性的依赖使用属性注入。

68

Q: 在XML中声明DI时,会有多种可选的配置方案和风格。具体到构造器注入,有两种基本的配置方案可供选择:
A:

  1. 元素
  2. 使用Spring 3.0所引入的c-命名空间

69

Q: 在Spring XML配置中,只有一种声明bean的方式
A: 使用元素并指定class属性。Spring会从这里获取必要的信息来创建bean。

70

Q: JavaConfig要优于XML配置的部分原因。
A:

  1. 在xml中配置bean,方式单一 在XML配置中,bean的创建显得更加被动,不过,它并没有JavaConfig那样强大,在JavaConfig配置方式中,你可以通过任何可以想象到的方法来创建bean实例。
  2. Spring的XML配置并不能从编译期的类型检查中受益。 另外一个需要注意到的事情就是,在一个简单的声明中,我们将bean的类型以字符串的形式设置在了class属性中。无法保证设置给class属性的值是真正的类。Spring的XML配置并不能从编译期的类型检查中受益。即便它所引用的是实际的类型,如果你重命名了类,会发生错误
Sort:  

Hello @huaiying! This is a friendly reminder that you have 3000 Partiko Points unclaimed in your Partiko account!

Partiko is a fast and beautiful mobile app for Steem, and it’s the most popular Steem mobile app out there! Download Partiko using the link below and login using SteemConnect to claim your 3000 Partiko points! You can easily convert them into Steem token!

https://partiko.app/referral/partiko

Thank you for reminding me, I have already dealt with it.

I was too busy to work before, and I have not had time to reply. I am very sorry.

No worries at all! Hope to see you in Partiko soon!

Posted using Partiko Android

Coin Marketplace

STEEM 0.14
TRX 0.23
JST 0.031
BTC 84230.47
ETH 2080.32
USDT 1.00
SBD 0.63