Spring 的autowired大坑

---恢复内容开始---

@autowired

自动注入大坑

这几天在spring的配置上花了许多时间....

首先appcontextconfig.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!--配置spring自动扫描的包-->

<context:component-scan base-package="z.talent"/>

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

<!-- 配置数据源 ,dbcp -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${mysql.driver}"/>
<property name="url" value="${mysql.userurl}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
<property name="maxActive" value="30"/>
<property name="maxIdle" value="5"/>
</bean>

 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatisconfig.xml" />
<!-- 扫描entity包 使用别名 -->

<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>

<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="z.talent.mapper" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 遍历所有的类 官方解释 写上这句会自动注册 指定包的class(@Component, @Repository, @Service, @Controller, @RestController,
@ControllerAdvice, and @Configuration)为bean -->

<!-- 加载框架mvc注释驱动 我自己说的 反正这两句少了就是不行,不信你试试-->
<mvc:annotation-driven />
<!-- 静态资源 image javascirpt css 不设置这个url访问image/*.png 肯定返回404 因为没写这个给controller处理里面并没有处理这个当然返回404-->
<mvc:resources location="/WEB-INF/resource/" mapping="/resource/**"
cache-period="864000" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 静态资源访问处理 -->
<mvc:default-servlet-handler/>
</beans>

 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>

>>>>>>>>
>>>>>>..

>>>>>

>>>

>>

>

 

紧接着呢

》》》

开始写dao层

首先我要说明下其实交给spring管理了,就没有必要自己实现dao接口了

mybatis的mapper就相当于dao层了,,,

接着都是spring的事了

然后嘞就是啥呢

然后开始注入service层

@autowired的mapper

service层自动注入了mapper

那么使用service的地方也要@autowired 这个service

autowired 不能断,要一直应用下去

不然会爆空指针错误  这里指的是 mapper

哈哈哈哈哈哈哈

 

期间呢还发生了需要故事

如果报了 logger 啥的错误  type different class类似字眼记得

把tomcat下bin的commed log 。jar删了

 

xxiixixixixixihahahaha

哈哈哈哈

转载于:https://www.cnblogs.com/zhangtalent/p/8450085.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值