Springѧϰ֮·1

2020-03-17 16:10:08À´Ô´£º²©¿ÍÔ° ÔĶÁ ()

ÐÂÀÏ¿Í»§´ó»ØÀ¡,ÔÆ·þÎñÆ÷µÍÖÁ5ÕÛ

Springѧϰ֮·1

Spring ¿ò¼Ü

¼ò½é

Spring¿ò¼ÜÊÇÓÉÓÚÈí¼þ¿ª·¢µÄ¸´ÔÓÐÔ¶ø´´½¨µÄ

  • Ä¿µÄ£º½â¾öÆóÒµÓ¦Óÿª·¢µÄ¸´ÔÓÐÔ

  • ¹¦ÄÜ£ºÊ¹Óûù±¾µÄJavaBean´úÌæEJB£¬²¢ÌṩÁ˸ü¶àµÄÆóÒµÓ¦Óù¦ÄÜ

  • ·¶Î§£ºÈκÎJavaÓ¦ÓÃ

Óŵ㣺

¿ªÔ´µÄÃâ·ÑµÄ¿ò¼Ü£¨ÈÝÆ÷£©

SpringÊÇÒ»¸öÇáÁ¿¼¶¡¢·ÇÈëÇÖʽµÄ¿ò¼Ü

¿ØÖÆ·´×ª(IOC)ºÍÃæÏòÇÐÃæ(AOP)µÄÈÝÆ÷¿ò¼Ü

Ö§³ÖÊÂÎñµÄ´¦Àí£¬¶Ô¿ò¼ÜÕûºÏµÄÖ§³Ö

¹ÙÍø£ºhttps://spring.io/

GitHub£ºhttps://github.com/spring-projects/spring-framework


Maven

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>

spring Æß´óÄ£¿é

IOC ÀíÂÛ

IoC È«³ÆΪ Inversion of Control£¬¼´¡°¿ØÖÆ·´×ª¡±£¬Spring µÄºËÐÄÄÚÈÝÖ®Ò»£¬ÊÇÒ»ÖÖͨ¹ýÃèÊö£¨xml »ò ×¢½â£©²¢Í¨¹ýµÚÈý·½»ñÈ¡Ìض¨¶ÔÏóµÄ·½Ê½£¬Spring ÖÐʵÏÖ¿ØÖÆ·´×ªµÄÊÇ IoC ÈÝÆ÷£¬ÆäʵÏÖ·½Ê½ÊÇ DI£¨Dependency Injection£©,¼´ÒÀÀµ×¢Èë¡£

  1. Ë­¿ØÖÆË­£ºÔÚ´«Í³µÄ¿ª·¢Ä£Ê½Ï£¬ÎÒÃǶ¼ÊDzÉÓÃÖ±½Ó new Ò»¸ö¶ÔÏóµÄ·½Ê½À´´´½¨¶ÔÏó£¬Ò²¾ÍÊÇ˵ÄãÒÀÀµµÄ¶ÔÏóÖ±½ÓÓÉÄã×Ô¼º¿ØÖÆ£¬µ«ÊÇÓÐÁË IOC ÈÝÆ÷ºó£¬ÔòÖ±½ÓÓÉ IoC ÈÝÆ÷À´¿ØÖÆ¡£ËùÒÔ¡°Ë­¿ØÖÆË­¡±£¬µ±È»ÊÇ IoC ÈÝÆ÷¿ØÖƶÔÏó¡£
  2. ¿ØÖÆʲô£º¿ØÖƶÔÏó¡£
  3. ΪºÎÊÇ·´×ª£ºÃ»ÓÐ IoC µÄʱºòÎÒÃǶ¼ÊÇÔÚ×Ô¼º¶ÔÏóÖÐÖ÷¶¯È¥´´½¨±»ÒÀÀµµÄ¶ÔÏó£¬ÕâÊÇÕýת¡£µ«ÊÇÓÐÁË IoC ºó£¬ËùÒÀÀµµÄ¶ÔÏóÖ±½ÓÓÉ IoC ÈÝÆ÷´´½¨ºó×¢Èëµ½±»×¢ÈëµÄ¶ÔÏóÖУ¬ÒÀÀµµÄ¶ÔÏóÓÉÔ­À´µÄÖ÷¶¯»ñÈ¡±ä³É±»¶¯½ÓÊÜ£¬ËùÒÔÊÇ·´×ª¡£
  4. ÄÄЩ·½Ã淴תÁË£ºËùÒÀÀµ¶ÔÏóµÄ»ñÈ¡±»·´×ªÁË¡£

IoC´´½¨¶ÔÏó

ͨ¹ý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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="he" class="com.youzi.pojo.Hello">
        <property name="Str" value="Hello Spring!"/>
    </bean>
</beans>
public class Hello {
    private String Str;

    public Hello(String str) {
        Str = str;
    }

    public Hello() {
    }

    public String getStr() {
        return Str;
    }

    public void setStr(String str) {
        Str = str;
    }
}
...
@Test
public void test() {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Object he = context.getBean("he");
    System.out.println(he);   
}

ÒýÓÃÁËÆäËû¶ÔÏóµÄ¸³Öµ·½Ê½£º

<bean id="userservice" class="com.youzi.service.UserServiceImpl">
    <property name="user" ref="user"/>
</bean>

ûÓвÎÊýÖ±½Ó³õʼ»¯

<bean id="user" class="com.youzi.dao.UserImpl"/>

ÒÔÉÏ·½Ê½µ÷ÓÃÁ˶ÔÓ¦ÀàÖеÄÎ޲ι¹ÔìºÍ setter£¬¼´ÐèÒª³õʼ»¯µÄ²ÎÊý»òÕ߶ÔÏó±ØÐëÓжÔÓ¦µÄ setter£¬Èç¹ûÒªµ÷ÓÃÓвι¹Ôì¿ÉÒÔʹÓà <constructor-arg> ͨ¹ýÀàÐÍ¡¢Ï±ꡢ²ÎÊýÃû×Ö¡¢ÒýÓÃÕ⼸ÖÖ·½Ê½´«²Î

ʹÓù¹ÔìÆ÷

public Users(Hello hello, String num) {
    this.hello = hello;
    this.num = num;
}
<bean id="users" class="com.youzi.pojo.Users">
    <!--<constructor-arg type="java.lang.String" value="1"/>-->
    <!--<constructor-arg index="1" value="2"/>-->
    <constructor-arg name="num" value="3"/>
    <constructor-arg ref="he"/>
</bean>

spring»ù±¾ÅäÖÃ

±ðÃû£¬¸ø¶ÔÏó´´½¨ÁíÒ»¸öÃû×Ö£¬¿É´´½¨¶à¸ö£¨±ðÃûÒ²¿ÉÒÔͨ¹ý bean ÖÐµÄ name ´´½¨£©

<bean id="he" class="com.youzi.pojo.Hello" name="hello3,hello4">
    <property name="Str" value="Hello Spring!"/>
</bean>

<alias name="he" alias="hello1"/>
<alias name="he" alias="hello2"/>

bean ¶ÔÏóĬÈÏ´´½¨µÄΪµ¥Àýģʽ£¬¿ÉÒÔͨ¹ý scope="prototype" Ð޸ģ¬ÕâÑù hello3,hello4 ¾ÍÊÇÁ½¸ö²»Í¬µÄ¶ÔÏóÁË

µ¼ÈëÆäËûµÄxml

ÉÏÃæÖ»ÊÇ»ù±¾µÄÅäÖã¬Ã¿¸öÅäÖÃÀïÃ滹ÓкܶàµÄϸ½ÚÅäÖôýÒÔºóÔÙ½øÒ»²½µÄѧϰ



Ô­ÎÄÁ´½Ó:https://www.cnblogs.com/wangjr1994/p/12514704.html
ÈçÓÐÒÉÎÊÇëÓëÔ­×÷ÕßÁªÏµ

±êÇ©£º

°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»­£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ­×÷ÕßÁªÏµ£¬°æȨ¹éÔ­×÷ÕßËùÓÐ

ÉÏһƪ£ºQuartzÈëÃÅ (¶þ) SimpleTrigger

ÏÂһƪ£º¡¾ÊÓƵ+ͼÎÄ¡¿Java»ù´¡¾­µäÁ·Ï°Ì⣨һ£©Êä³ö2-100Ö®¼äµÄËØÊý£¬¼°