銆怱pring銆?7 鍚庣画鐨勫涔犺ˉ鍏?vol1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆怱pring銆?7 鍚庣画鐨勫涔犺ˉ鍏?vol1相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/tst' title='tst'>tst   琛ュ厖   ack   sql   瀹瑰櫒   property   nfa   str   rod   

鎺у埗鍙嶈浆Inverse Of Control鐨勬紨鍙橈細

鍦ㄤ箣鍓嶇殑鍘熺敓Javaweb椤圭洰鐨勯棶棰橈細

鎴戜滑涓夊眰鏋舵瀯姣忎竴灞備箣闂寸殑鑱旂郴鏄繖鏍风殑锛?/p>

 

鐢盙radeDao鎺ュ彛鎸囧悜GradeDaoImpl

鎶€鏈浘鐗? src=

鍐嶇敱GradeService鎸囧悜GradeServiceImpl

鎶€鏈浘鐗? src=

浣嗘槸鎴戜滑鍙戠幇锛屽鏋滆繖鏍峰啓姝诲湪姣忎竴灞傜殑纭紪鐮佷腑鐨勮瘽锛岄偅涔堟帴鍙g殑瀛樺湪灏卞け鍘讳簡鎰忎箟

瀵硅薄鐨勫垱寤烘槸鐢辫繖涓笂灞傜殑鍐呴儴鎵€鍐冲畾

 

鎴戜滑鏄笇鏈涙瘡涓€灞傜殑鑱旂郴鏈€灏忓寲锛屼絾鏄張涓嶈兘瀹屽叏闅旂粷鍏崇郴锛岀粍浠跺氨鍍忕粍瑁呯殑濂界殑闆朵欢

鐩镐簰涔嬮棿鐨勪緷璧栭檷浣庡埌鏈€灏忥紝姣斿杩欎釜daoImpl鎴戦渶瑕佹浛鎹紝灏辨牴鎹疄鐜扮殑dao鎺ュ彛灏卞彲浠ユ湁

mysql瀹炵幇锛孫racle瀹炵幇锛孭ostgreSQL瀹炵幇绛夌瓑

 

瀵硅薄鐨勫垱寤哄彲浠ョЩ浜ゅ埌澶栭儴锛屾垜浠湪鍐呴儴璁剧疆setter娉ㄥ叆鍗冲彲锛屾垨鑰呬娇鐢ㄥ甫鍙傛瀯閫犲櫒

鎶€鏈浘鐗? src=

 

閫氳繃setter鎴栬€呭甫鍙傛瀯閫犲櫒锛屾垜浠彲浠ヤ粠鏈眰鐨勫唴閮ㄥ垱寤哄璞¤繖涓帶鍒舵潈鍔涳紝绉讳氦鍒颁簡澶栭潰

铏界劧杩欒繕鏄‖缂栫爜涓?/p>

鎺ヤ笅鏉ユ垜浠氨鍒涘缓涓€涓伐鍘傜被锛岀敱宸ュ巶瀹炵幇瀵硅薄鐨勫垱寤猴紝鑰岃繖涓涓猴紝闈炲父鍍忕敓浜т骇鍝?/p>

涓€涓釜瀵硅薄鍗虫垜浠殑涓氬姟鐨勪骇鍝?/p>

鎶€鏈浘鐗? src=

瀵硅薄鐨勬敞鍏ヤ氦鐢卞伐鍘傜敓浜х粰浜?/p>

鎶€鏈浘鐗? src=

浣嗘槸鎴戜滑鏄笉鏄彂鐜帮紝宸ュ巶鐨勪骇鍝佽繕鏄浐瀹氱殑锛熺劧鍚庡啀鍍廕dbc涓€鏍凤紝

閫氳繃璇诲彇閰嶇疆鏂囦欢鐨勫舰寮忚В闄よ€﹀悎

 

棣栧厛鏄厤缃枃浠讹細bean.properties

渚嬪杩欓噷鎴戝氨閰嶇疆鍙﹀涓€绉嶅疄鐜?/p>

AaaDao = ioc.dao.AaaDaoImpl2
AaaService = ioc.service.AaaServiceImpl2

杩欓噷鍙槸閿€煎绠€鍗曠殑瀛楃涓?/p>

 

鐒跺悗鏀归€犳垜浠殑BeanFactory

閫氳繃鍔犺浇鐨勬椂鍊欒鍙栭厤缃枃浠剁洿鎺ョ敓鎴愬璞℃斁杩涜繖涓猰ap瀹瑰櫒涓?/p>

杩欎釜瀹瑰櫒涓嶅氨鏄痓ean鐨勫鍣ㄤ簡鍚楋紝閿搴旂殑瀹炵幇绫诲悕锛屽€煎嵆瀵硅薄

鎶€鏈浘鐗? src=

put鏀捐繘鍘荤殑key搴旇鎹alue杩涘幓銆傘€傘€傛垜鏈夌偣鍒嗕笉娓?/span>

 

浣嗚繖涓嶆槸閲嶇偣锛屾垜浠笅闈㈣幏鍙栧璞$殑鏂规硶灏辨槸浠庡鍣ㄩ噷闈㈣幏鍙?/p>

杩欏氨闇€瑕乲ey浣滀负鍙傛暟锛?/p>

鍐嶅姞涓婃硾鍨嬶紝杩欐牱鎴戜滑灏变笉闇€瑕佽繘琛屽己杞簡

鎶€鏈浘鐗? src=

 

浣嗘槸鏈変釜闂灏辨槸dao涔熸槸鎺ュ彛鍟婏紝鎺ュ彛鎸囧悜浜嗕箣鍚庢病鍔炴硶鍦╯etter浜嗭紝浣嗘槸鍙互涓嶈缃帴鍙e鐞?/p>

className搴旇鎹㈡垚鍙玝eanName

鎶€鏈浘鐗? src=

鐜板湪鎴戜滑閫氳繃閰嶇疆鏂囦欢鐨勬柟寮忔潵鑾峰彇瀵硅薄锛屾暣涓繃绋嬪畬鍏ㄧ湅涓嶅埌涓€涓猲ew瀛椼€?/p>

 

鎬荤粨锛氬啓鐨勫ソ鐑傦紝杩囩▼鏀逛簡濂藉嚑鍥?/p>

 

鐬庢壇鐨勪唬鐮侊細

 

Bean宸ュ巶

package ioc;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author DaiZhiZhou
 * @file OA-Project
 * @create 2020-07-24 22:57
 */
public class BeanFactory {

    private static Map<String,Object> beanContainer = new HashMap<>();

    static {
        try {
            // InputStream inputStream = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
銆€銆€銆€銆€銆€銆€銆€// 鍥犱负鎵句笉鍒伴厤缃枃浠跺氨妫€鏌ヤ笅璺緞鐨勮鍙?span style="color: #000000;">
            String file = BeanFactory.class.getClassLoader().getResource("bean.properties").getFile();
            System.out.println(file);
            InputStream inputStream = new FileInputStream(new File(file));

            Properties properties = new Properties();
            properties.load(inputStream);

            Enumeration<Object> keys = properties.keys();

            while (keys.hasMoreElements()) {
                String key = (String)keys.nextElement();
                String value = properties.getProperty(key);

                Class<?> classByValue = Class.forName(value);
                Object valueInstance = classByValue.newInstance();
                beanContainer.put(key, valueInstance);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static <Bean>Bean getBean(String className, Class<Bean> beanClass) {
        Object o = beanContainer.get(className);
        return (Bean)o;
    }

}

properties:

AaaDao = ioc.dao.AaaDaoImpl2
AaaService = ioc.service.AaaServiceImpl2

娴嬭瘯绫伙細鍋囪鏄鍥剧殑Servlet

package ioc.servlet;

import ioc.BeanFactory;
import ioc.dao.AaaDao;
import ioc.dao.AaaDaoImpl;
import ioc.service.AaaService;
import ioc.service.AaaServiceImpl;
import ioc.service.AaaServiceImpl2;
import org.junit.Test;

/**
 * @author DaiZhiZhou
 * @file OA-Project
 * @create 2020-07-24 22:48
 */
public class AaaServletFake {

    private AaaService aaaService;

    public void setAaaService(AaaService aaaService) {
        this.aaaService = aaaService;
    }

    @Test
    public void toAaaListPage() {
        setAaaService(new AaaServiceImpl(new AaaDaoImpl()));
        aaaService.getAaaList();
    }

    @Test
    public void toAaaListPageByFactoryProduct() {
        //this.setAaaService(BeanFactory.getAaaService());
        aaaService.getAaaList();
    }

    @Test
    public void toAaaListPageByFactoryProductPropertyFileInject() {
        AaaDao aaaDao = BeanFactory.getBean("AaaDao", AaaDao.class);
        AaaServiceImpl2 aaaServiceImpl2 = BeanFactory.getBean("AaaService", AaaServiceImpl2.class);
        aaaServiceImpl2.setAaaDao(aaaDao);
        this.setAaaService(aaaServiceImpl2);
        this.aaaService.getAaaList();
    }

}

 

鍏充簬娉ㄥ叆闂鐨勫啀琛ュ厖锛?/h2>

鍥犱负瀛樺湪杩欑宸ュ巶鏂瑰紡鐨勬敞鍏ワ紝鎵€浠pring灏变篃鎻愪緵浜嗗搴旂殑杩欑鏂瑰紡锛?/p>

闈欐€佸伐鍘傝幏鍙?nbsp;鍜?nbsp;宸ュ巶瀹炰緥鏂规硶鑾峰彇锛?/h3>

璇︾粏瑙侊細

http://123.57.58.29/blog/1591625710261

 

闈欐€佸伐鍘?/p>

    <!-- 姝ょ鏂瑰紡鏄?  
     浣跨敤 StaticFactory 绫讳腑鐨勯潤鎬佹柟娉?createAccountService 鍒涘缓瀵硅薄锛屽苟瀛樺叆 spring 瀹瑰櫒 
       id 灞炴€э細鎸囧畾 bean 鐨?id锛岀敤浜庝粠瀹瑰櫒涓幏鍙?  
       class 灞炴€э細鎸囧畾闈欐€佸伐鍘傜殑鍏ㄩ檺瀹氱被鍚?  
       factory-method 灞炴€э細鎸囧畾鐢熶骇瀵硅薄鐨勯潤鎬佹柟娉? 
     -->
    
       <bean id="accountService" class="com.tc51.factory.StaticFactory" factory-method="createAccountService"></bean>

瀹炰緥宸ュ巶

銆€銆€<!--鍒涘缓宸ュ巶瀵硅薄-->
    <!--鍏堥€氳繃瀹炰緥宸ュ巶鍒涘缓瀵硅薄-->
    <bean id="bean3Factory" class="com.tc51.factory.Bean3Factory"></bean>
    <bean id="accountService" factory-bean="bean3Factory" factory-method="createAccountService"></bean>

 

以上是关于銆怱pring銆?7 鍚庣画鐨勫涔犺ˉ鍏?vol1的主要内容,如果未能解决你的问题,请参考以下文章

绋嬪簭鐨勬満鍣ㄧ骇琛ㄧず

澶╃尗棣栭〉UI灞傛涓庣被璁捐鍒嗘瀽

闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢

/etc/profile銆?etc/bashrc銆亊/.bash_profile銆亊/.bashrc 鐨勫尯鍒紙杞級

绗旇瘯-4399銆?020鏍℃嫑銆慦eb鍚庣寮€鍙戝伐绋嬪笀绗旇瘯棰橈紙鎴戝張琛屼簡锛熺劧鍚庝竴浠介潰璇曢€氱煡閮芥病鏈夛紝鎴戞槸鐪熺殑鑿滃晩銆傘€傘€傘€傦級