Spring framework IOC (inversion control) and DI (dependency injection)

IDEA run error loading the document

Written maven project occasional error when running IDEA

After analysis and Baidu, the wrong reasons: the project is not dependent on packaging, IDEA can not load a local file, but networking query.
Solution: Modify the configuration file maven pom.xml project, add the following configuration code to add plug-in build Tags:
<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
      </plugin>
 </plugins>
Add the following code is added at the beginning of plug disposed at pluginManagement tags:
<plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>2.4.3</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
              <configuration>
                <transformers>
                  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    <mainClass>com.lanou3g.spring.App</mainClass>
                  </transformer>
                  <transformer
                          implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <resource>META-INF/spring.schemas</resource>
                  </transformer>
                  <transformer
                          implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <resource>META-INF/spring.handlers</resource>
                  </transformer>
                </transformers>
              </configuration>
            </execution>
          </executions>
</plugin>

Lazy loading

IOC container default at startup will initialize all the bean configuration file, when the code is not our mistake, but mistakes can be quickly loaded container tips to help us exclude coding errors, this is called fail-fast, without modification in most cases, but we do not need a special time when the container is loaded on startup bean object, such as a project of a feature not commonly used, load so many resources when we do not need to start the project, the project will reduce wasteful time-sensitive, you need to set the bean objects lazy loading, is the only call it when (getBean) and then load it, which would be subject to the bean plus lazy-init attribute, attribute value is true, the default is false, generally do not write as follows:

 <bean id="studentDao" class="com.lanou3g.spring.dao.StudentDaoImpl" lazy-init="true"/>

Use annotations to introduce XML configuration

In the case of multiple profiles, we will introduce in a configuration file additional configuration file in XML is to use the Import label introducing additional configuration file, as follows:

<import resource="applicationContext.xml"/>

The introduction of the XML configuration file with annotations ways to do this:

@ImportResource("applicationContext.xml")

The role of these two approaches are the same, any one can grasp.

bean's name property

Since the name attribute is not common bean, is not too much to explain here, and its role as the id attribute, to the current bean objects marked facilitate access to a name, except that the name attribute values ​​may be provided to make a plurality of names, between the comma separated, note, from the plurality of names can not be repeated, and not to any other bean name attribute value is repeated.

<bean id="student" name="stu,student1,sd1" class="com.lanou3g.spring.bean.Student">

Injection anonymous inner bean

Injection anonymous inner bean this way corresponds to the object assigned to the object assignment, there are two ways, one is through the new object, and then set the properties of the object to the object property assignment, transmission parameters by one is parameterized constructor assigned to the object in the bean configuration file created by an object can be assigned to objects, practices are as follows:

<bean id="outer" class="...">
    <!-- 像set一样实例化对象的方式用这样的注入方式 -->
    <property name="target">
        <bean class="com.example.Person"> <!-- this is the inner bean -->
            <property name="name" value="Fiona Apple"/>
            <property name="age" value="25"/>
        </bean>
    </property>
  	
  	<!-- 像带参构造函数实例化对象的方式用这样的注入 -->
    <constructor-arg name="target">
        <bean class="com.example.Person"> <!-- this is the inner bean -->
            <property name="name" value="Fiona Apple"/>
            <property name="age" value="25"/>
         </bean>
    </constructor-arg>
</bean>

Injection type attribute set

Injected interlinked nature, are assigned, do not explain, list injection method, Map injection method:

 <!-- lsit注入 -->
<property name="someList">
        <list>
            <!-- 普通元素 -->
          	<value>a list element followed by a reference</value>
            <!-- 引用类型元素 -->
          	<ref bean="myDataSource" />
        </list>
</property>
 <!-- Map注入 -->
<property name="someMap">
        <map>
          	<entry key="an entry" value="just some string"/>
            <entry key ="a ref" value-ref="myDataSource"/>
        </map>
</property>

And injecting null null string attribute value

<bean class="ExampleBean">
    <property name="email" value=""/>
</bean>

<bean class="ExampleBean">
    <property name="email">
         <!-- 注意这里注入null必须写这样的标签形式,测试时报空指针,value=“null”不行,编译器把它当字			   符串,长度为4 -->
        <null/>
    </property>
</bean>

Injection composite attribute value

<bean id="something" class="things.ThingOne">
    <property name="fred.bob.sammy" value="123" />
</bean>

C injection namespace configuration parameters

C namespace and the nature of the traditional injection method as set the same way as an instance of an object, only need to modify the configuration file header information
Can by C: injecting property value, than the traditional injection method is simple and convenient
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         <!-- 这里需要配置 -->
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="beanTwo" class="x.y.ThingTwo"/>
    <bean id="beanThree" class="x.y.ThingThree"/>
    <!-- c命名空间方式注入构造参数 -->
    <bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
        c:thingThree-ref="beanThree" c:email="[email protected]"/>
</beans>

P injection namespace configuration parameters

P namespace like nature and manner of a conventional injection-argument constructor of the object with the same instance, only need to modify the configuration file header information
We can, through P: injecting property value, than the traditional injection method is simple and convenient
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      <!-- 这里需要配置 -->
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="driver" class="com.demo.Driver" p:car-ref="car" p:dname="张三" />
  <bean id="car" class="com.demo.Car" />
</beans>

PropertyPlaceholderConfigurer injection tools Properties file attributes

Using the following injection mode, when the IOC container starts various parameter values ​​will load the database connection, more efficient, since when the need to change the database, only need to modify the value jdbc.properties file.

<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          p:driverClassName="${jdbc.driver.className}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}"
          p:maxIdle="${jdbc.maxIdle}"
          p:minIdle="${jdbc.minIdle}"
          p:maxActive="${jdbc.maxActive}"
    />

jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
jdbc.username=root
jdbc.password=root
jdbc.maxIdle=3
jdbc.minIdle=1
jdbc.maxActive=10

Guess you like

Origin blog.csdn.net/weixin_43311054/article/details/92110965