该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
-----------------------------------------------------------------------
--spring:java Bean管理(自动装配+自动扫描)
-----------------------------------------------------------------------
spring作为一个IOC容器,可以实现对java Bean容器的自动管理,这一点让我作为c#程序员有点惭愧。
因为我写了好多年的c#代码,对于类对象也没怎么使用到容器技术,一直都是new来new去。
-----------------------------------------------------------------------自动装配
spring自动装配,就是你在applicationContext.xml中配置好相关bean,spring在容器初始化时,会自动去根据配置文件中的配置进行对象实例化。
示例:applicationContext-dataSource.xml
-----------------------------------------------------------------------自动扫描
示例:applicationContext.xml
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
Spring公共配置
//------------------------------------------------Dao层类示例
类注解:
@controller(给web层的注解)
@service(给serivce层加的注解)
@repository(给dao层加的注解)
@component(给java类加注解,老版本spring只有这一个注解)
说明:如果类没有被注解,是扫描不到的。
@Repository
public class Log2Dao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void insert()
{
String sql = "insert into tlog (logtitle,logcontent) values (?,?)";
jdbcTemplate.update(sql,"luozhiping","luozhiping");
}
}