最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IDEA中如何引入spring的命名空間

 更新時(shí)間:2023年04月11日 10:48:47   作者:yuehailin  
這篇文章主要介紹了IDEA中如何引入spring的命名空間問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

IDEA引入spring的命名空間

我們?cè)趯憇pring的配置文件的時(shí)候,有的時(shí)候可能會(huì)用到 P 標(biāo)簽,然后我們發(fā)現(xiàn)自己并沒有p標(biāo)簽啊,那么我們一起來(lái)看我是怎么解決的。

首先在我們的xml文件的首部添上這句話:

xmlns:context="http://www.springframework.org/schema/context"

然后我們打出

xmlns:p=

然后就會(huì)相應(yīng)的提示:

還有一點(diǎn)需要注意的就是:

需要注意的是必須在xmlns:context="”這一行的下面打,否則也不會(huì)提示,如圖所示位置即可提示,否則可能不提示

最終的代碼:

xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

idea項(xiàng)目添加spring

配置步驟

1.添加spring的依賴包

idea可以直接右擊項(xiàng)目 選擇add frame support,勾選spring即可

2.創(chuàng)建applicationContext.xml

在src的直接子目錄下創(chuàng)建 applicationContext.xml

這里給出一個(gè)applicationContext.xml 的實(shí)例,以及注釋解釋

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd        
      http://www.springframework.org/schema/aop         
      http://www.springframework.org/schema/aop/spring-aop.xsd        
      http://www.springframework.org/schema/context         
      http://www.springframework.org/schema/context/spring-context.xsd        
      http://www.springframework.org/schema/tx         
      http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!-- 掃描有注解的文件 base-package 包路徑 -->
    <context:component-scan base-package="service.imp, action, dao.imp"/>


    <!-- 定義 Autowired 自動(dòng)注入 bean -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>


    <!-- 聲明式容器事務(wù)管理 ,transaction-manager指定事務(wù)管理器為transactionManager -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*User"/>
            <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>
        </tx:attributes>
    </tx:advice>


    <!-- 定義切面,在service包及子包中所有方法中,執(zhí)行有關(guān)的hibernate session的事務(wù)操作 -->
    <aop:config>
        <!-- 只對(duì)業(yè)務(wù)邏輯層實(shí)施事務(wù) -->
        <aop:pointcut id="serviceOperation" expression="execution( * service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    </aop:config>


    <!-- 配置dataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl"
                  value="jdbc:mysql://localhost:3306/j2ee?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true"/>
        <property name="user" value="root"/>
        <property name="password" value="wyy"/>
        <property name="initialPoolSize" value="5"/>
        <property name="maxPoolSize" value="10"/>
    </bean>


    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="model"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL57Dialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">true</prop>
            </props>
        </property>
    </bean>

    <!-- 配置hibernateTemplate -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

</beans>

3.給service的實(shí)現(xiàn)類添加@Service注解 給dao的實(shí)現(xiàn)類添加@Repository注解 將生命周期管理交給spring

注意所有交給spring管理的類,不能new出實(shí)例,只能用spring注入。

4.所有使用到service和dao的地方,均使用@Autowired注解注入。

@Autowired注解可以在構(gòu)造函數(shù)、類成員屬性、getset方法添加注解注入bean,但是類成員屬性的注入方法是不推薦的

在stackoverflow上有人做了詳細(xì)的解釋 https://stackoverflow.com/questions/39890849/what-exactly-is-field-injection-and-how-to-avoid-it

總結(jié)下來(lái),使用屬性注入會(huì)產(chǎn)生如下問(wèn)題

  • 對(duì)象和注入的容器有著很緊的耦合
  • 對(duì)象間的耦合被隱藏了,外部無(wú)法看到,不利于復(fù)雜度控制
  • 如果沒有注入容器,對(duì)象無(wú)法創(chuàng)建
  • 當(dāng)一個(gè)類有多個(gè)屬性注入,你感知不到他的復(fù)雜度。而當(dāng)你使用構(gòu)造函數(shù)注入時(shí),就會(huì)發(fā)現(xiàn),要穿入的參數(shù)過(guò)多。也是不利于復(fù)雜度控制

5.dao的實(shí)現(xiàn)技術(shù)

  • sessionFactory
@Repository
public class UserDaoImp implements UserDao {

    private SessionFactory sessionFactory;

    @Autowired
    public UserDaoImp(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public User get(String userId) {
        return sessionFactory.openSession().load(User.class, userId);
    }
}
  • hibernateTemplate
@Repository
public class UserDaoImp implements UserDao {

    @Autowired
    private HibernateTemplate hibernateTemplate;

    public UserDaoImp(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public User get(String userId) {
        return hibernateTemplate.get(User.class, userId);
    }
}

hibernateTemplate封裝了SessionFactory,數(shù)據(jù)庫(kù)操作變得更簡(jiǎn)單。

如下給出實(shí)現(xiàn)hibernateTemplate分頁(yè)的代碼。

@Override
public List<Order> getListByHql(String hql, int page, int pageSize) {
    return hibernateTemplate.execute(new HibernateCallback<List<Order>>() {
        @Override
        public List<Order> doInHibernate(Session session) throws HibernateException {
            Query<Order> query = session.createQuery(hql);
            query.setFirstResult((page - 1) * pageSize).setMaxResults(pageSize);
            //把結(jié)果返回
            return query.list();
        }
    });
}

問(wèn)題與解決

nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

這個(gè)錯(cuò)誤顯然是沒有找到某個(gè)jar包。如果要定義aop,除了spring核心包之外,還需要自行下載這兩個(gè)jar。

  • aopalliance.jar
  • aspectjweaver.jar

檢查一下jar包,發(fā)現(xiàn)沒有aspectjweaver.jar,下載并加入到項(xiàng)目路徑即可。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決@Transaction注解導(dǎo)致動(dòng)態(tài)切換更改數(shù)據(jù)庫(kù)失效問(wèn)題

    解決@Transaction注解導(dǎo)致動(dòng)態(tài)切換更改數(shù)據(jù)庫(kù)失效問(wèn)題

    這篇文章主要介紹了解決@Transaction注解導(dǎo)致動(dòng)態(tài)切換更改數(shù)據(jù)庫(kù)失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 詳解如何有效地處理Java中的多線程

    詳解如何有效地處理Java中的多線程

    在現(xiàn)代軟件開發(fā)中,多線程編程已成為提高程序性能和響應(yīng)速度的重要手段,Java提供了豐富的多線程支持,使得在Java中實(shí)現(xiàn)并發(fā)操作變得相對(duì)簡(jiǎn)單,本文將深入探討Java多線程編程的基本概念、常見問(wèn)題和最佳實(shí)踐,需要的朋友可以參考下
    2024-06-06
  • SpringBoot搭建全局異常攔截

    SpringBoot搭建全局異常攔截

    這篇文章主要介紹了SpringBoot搭建全局異常攔截,本文通過(guò)詳細(xì)的介紹與代碼的展示,詳細(xì)的說(shuō)明了如何搭建該項(xiàng)目,包括創(chuàng)建,啟動(dòng)和測(cè)試步驟,需要的朋友可以參考下
    2021-06-06
  • Java 常見的幾種內(nèi)存溢出異常的原因及解決

    Java 常見的幾種內(nèi)存溢出異常的原因及解決

    這篇文章主要介紹了Java 常見的幾種內(nèi)存溢出異常及解決,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-04-04
  • 詳解如何使用SpringBoot封裝Excel生成器

    詳解如何使用SpringBoot封裝Excel生成器

    在軟件開發(fā)過(guò)程中,經(jīng)常需要生成Excel文件來(lái)導(dǎo)出數(shù)據(jù)或者生成報(bào)表,為了簡(jiǎn)化開發(fā)流程和提高代碼的可維護(hù)性,我們可以使用Spring Boot封裝Excel生成器,本文將介紹如何使用Spring Boot封裝Excel生成器,并提供一些示例代碼來(lái)說(shuō)明其用法和功能
    2023-06-06
  • 詳解Java回調(diào)的原理與實(shí)現(xiàn)

    詳解Java回調(diào)的原理與實(shí)現(xiàn)

    回調(diào)函數(shù),顧名思義,用于回調(diào)的函數(shù)。回調(diào)函數(shù)只是一個(gè)功能片段,由用戶按照回調(diào)函數(shù)調(diào)用約定來(lái)實(shí)現(xiàn)的一個(gè)函數(shù)。回調(diào)函數(shù)是一個(gè)工作流的一部分,由工作流來(lái)決定函數(shù)的調(diào)用(回調(diào))時(shí)機(jī)。
    2017-03-03
  • 基于logback.xml不生效問(wèn)題的解決

    基于logback.xml不生效問(wèn)題的解決

    這篇文章主要介紹了基于logback.xml不生效問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Spring Boot與Spark、Cassandra系統(tǒng)集成開發(fā)示例

    Spring Boot與Spark、Cassandra系統(tǒng)集成開發(fā)示例

    本文演示以Spark作為分析引擎,Cassandra作為數(shù)據(jù)存儲(chǔ),而使用Spring Boot來(lái)開發(fā)驅(qū)動(dòng)程序的示例。對(duì)spring boot 與spark cassandra集成開發(fā)示例代碼感興趣的朋友跟著腳本之家小編一起學(xué)習(xí)吧
    2018-02-02
  • SpringBoot優(yōu)化啟動(dòng)速度的方法實(shí)現(xiàn)

    SpringBoot優(yōu)化啟動(dòng)速度的方法實(shí)現(xiàn)

    本篇文章主要介紹了SpringBoot優(yōu)化啟動(dòng)速度的方法實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Spring中FactoryBean的高級(jí)用法實(shí)戰(zhàn)教程

    Spring中FactoryBean的高級(jí)用法實(shí)戰(zhàn)教程

    FactoryBean是Spring框架的高級(jí)特性,允許自定義對(duì)象的創(chuàng)建過(guò)程,適用于復(fù)雜初始化邏輯,本文給大家介紹Spring中FactoryBean的高級(jí)用法實(shí)戰(zhàn),感興趣的朋友跟隨小編一起看看吧
    2024-09-09

最新評(píng)論

万安县| 黎川县| 固阳县| 泰州市| 当阳市| 苏尼特左旗| 方正县| 五寨县| 白城市| 方正县| 花莲县| 阿巴嘎旗| 雅安市| 蓬莱市| 方正县| 昌黎县| 城市| 边坝县| 岱山县| 永靖县| 洛川县| 商洛市| 霞浦县| 锦屏县| 琼中| 射阳县| 长沙市| 宁强县| 五大连池市| 兴和县| 承德县| 康保县| 恭城| 顺义区| 苍南县| 桐城市| 云浮市| 海阳市| 广德县| 宜春市| 康乐县|