温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

IDEA中怎么引入spring的命名空间

发布时间:2023-04-11 11:32:11 来源:亿速云 阅读:130 作者:iii 栏目:开发技术

这篇文章主要介绍“IDEA中怎么引入spring的命名空间”,在日常操作中,相信很多人在IDEA中怎么引入spring的命名空间问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”IDEA中怎么引入spring的命名空间”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    IDEA引入spring的命名空间

    我们在写spring的配置文件的时候,有的时候可能会用到 P 标签,然后我们发现自己并没有p标签啊,那么我们一起来看我是怎么解决的。

    首先在我们的xml文件的首部添上这句话:

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

    然后我们打出

    xmlns:p=

    然后就会相应的提示:

    IDEA中怎么引入spring的命名空间

    还有一点需要注意的就是:

    需要注意的是必须在xmlns:context="”这一行的下面打,否则也不会提示,如图所示位置即可提示,否则可能不提示

    最终的代码:

    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项目添加spring

    配置步骤

    1.添加spring的依赖包

    idea可以直接右击项目 选择add frame support,勾选spring即可

    2.创建applicationContext.xml

    在src的直接子目录下创建 applicationContext.xml

    这里给出一个applicationContext.xml 的实例,以及注释解释

    <?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 自动注入 bean -->
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    
    
        <!-- 声明式容器事务管理 ,transaction-manager指定事务管理器为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包及子包中所有方法中,执行有关的hibernate session的事务操作 -->
        <aop:config>
            <!-- 只对业务逻辑层实施事务 -->
            <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的实现类添加@Service注解 给dao的实现类添加@Repository注解 将生命周期管理交给spring

    注意所有交给spring管理的类,不能new出实例,只能用spring注入。

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

    @Autowired注解可以在构造函数、类成员属性、getset方法添加注解注入bean,但是类成员属性的注入方法是不推荐的

    IDEA中怎么引入spring的命名空间

    总结下来,使用属性注入会产生如下问题

    • 对象和注入的容器有着很紧的耦合

    • 对象间的耦合被隐藏了,外部无法看到,不利于复杂度控制

    • 如果没有注入容器,对象无法创建

    • 当一个类有多个属性注入,你感知不到他的复杂度。而当你使用构造函数注入时,就会发现,要穿入的参数过多。也是不利于复杂度控制

    5.dao的实现技术

    • 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,数据库操作变得更简单。

    如下给出实现hibernateTemplate分页的代码。

    @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);
                //把结果返回
                return query.list();
            }
        });
    }

    问题与解决

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

    这个错误显然是没有找到某个jar包。如果要定义aop,除了spring核心包之外,还需要自行下载这两个jar。

    • aopalliance.jar

    • aspectjweaver.jar

    检查一下jar包,发现没有aspectjweaver.jar,下载并加入到项目路径即可。

    到此,关于“IDEA中怎么引入spring的命名空间”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI