菜園子

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            7 Posts :: 1 Stories :: 31 Comments :: 0 Trackbacks

          2011年9月12日 #

               摘要: 關(guān)于Hibernate的查詢從數(shù)據(jù)庫(kù)映射到JavaBean     Hibernate除了HQL外,還支持SQL的查詢,API為createSQLQuery(sql),如果數(shù)據(jù)庫(kù)使用的是Oracle,由于數(shù)據(jù)庫(kù)表中的列都是大寫(xiě),所以在從resultset到j(luò)avabean的時(shí)候,需要完全匹配。 一般我們會(huì)用DTO或者作為DTO的Entity,無(wú)論是采用add...  閱讀全文
          posted @ 2014-08-27 15:08 GhostZhang 閱讀(10129) | 評(píng)論 (2)編輯 收藏

          升級(jí)Spring3.1RC2 和Hibernate4.0.0CR7遇到的一些問(wèn)題及解決

          Spring3.1RC2支持

          1. Quartz2

          2. Hibernate4,

          3. New HandlerMethod-based Support Classes For Annotated Controller Processing

          4. Consumes and Produces @RequestMapping Conditions

          5. Working With URI Template Variables In Controller Methods

          6. Validation For @RequestBody Method Arguments  //and so on....

          7. Spring MVC 3.1 的annotation可以參看下http://starscream.iteye.com/blog/1098880 

          Hibernate 4可以查看http://community.jboss.org/wiki/HibernateCoreMigrationGuide40 

          下面主要說(shuō)一下我在升級(jí)過(guò)程中遇到的一些問(wèn)題及解決辦法。

          Maven的repository始終無(wú)法升級(jí)到SpringRC2,可能服務(wù)器有問(wèn)題吧,目前暫時(shí)是從官方下載的整個(gè)SpringRC2的zip包。版本號(hào)是:3.1.0.RC2

          Hibernate可以從repository中升級(jí)到4.0.0.CR7,新增的依賴包有jandex-1.0.3.Final.jar,jboss-logging-3.1.0.CR2.jar,jboss-transaction-api_1.1_spec-1.0.0.Final.jar。

          Quartz升級(jí)到2.1.1,Ehcache-core升級(jí)到2.5.0

          Spring3.1取消了HibernateTemplate,因?yàn)镠ibernate4的事務(wù)管理已經(jīng)很好了,不用Spring再擴(kuò)展了。所以以前的Dao需要改寫(xiě),直接調(diào)用Hibernate 的Session進(jìn)行持久化。

          Spring的配置:

          sessionFactoryorg.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean換成org.springframework.orm.hibernate4.LocalSessionFactoryBean

          Spring的配置:

          <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>改為

          <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>

          EhCacheRegionFactory使用配置:

          <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

          使用Hibernate所有的openSession()改為getCurrentSession()

          Spring 的配置:Hibernate transactionManager從3改為4,如下:

          <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

                  <property name="sessionFactory" ref="sessionFactory"/>

              </bean>

          Spring @ResponseBody輸出是亂碼的問(wèn)題:原來(lái)使用的是:

          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  

          改為:

          <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  

          <property name = "messageConverters">

          <list>  

          <bean class = "org.springframework.http.converter.StringHttpMessageConverter">  

          <property name = "supportedMediaTypes">  

          <list>

          <value>text/plain;charset=UTF-8</value>

          </list>

          </property> 

          </bean>

          <bean class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  

          <property name = "supportedMediaTypes">  

          <list>

          <value>text/plain;charset=UTF-8</value>

          <value>application/json;charset=UTF-8</value>

          </list>  

          </property> 

          </bean>

          </list>  

          </property>  

          這樣比每個(gè)Controller都加上@RequestMapping(value = "/showLeft", method = RequestMethod.GET)
           produces = "text/plain; charset=utf-8"方便的多。

          Blob,以前配置:

          @TypeDefs({@TypeDef(name="clob",typeClass=ClobStringType.class),@TypeDef(name="blob",typeClass=BlobByteArrayType.class)})

          @Lob

          @Type(type="blob")

          public byte[] getPic() {

          return pic;

          }

          現(xiàn)在改為:

              @Lob

          public byte[] getPic() {

          return pic;

          }

          簡(jiǎn)單很多。

           

          l 待續(xù)。。。

          posted @ 2011-12-13 15:36 GhostZhang 閱讀(3368) | 評(píng)論 (2)編輯 收藏

          Shiro權(quán)限框架

          開(kāi)發(fā)系統(tǒng)中,少不了權(quán)限,目前java里的權(quán)限框架有SpringSecurity和Shiro(以前叫做jsecurity),對(duì)于SpringSecurity:功能太過(guò)強(qiáng)大以至于功能比較分散,使用起來(lái)也比較復(fù)雜,跟Spring結(jié)合的比較好。對(duì)于初學(xué)Spring Security者來(lái)說(shuō),曲線還是較大,需要深入學(xué)習(xí)其源碼和框架,配置起來(lái)也需要費(fèi)比較大的力氣,擴(kuò)展性也不是特別強(qiáng)。

          對(duì)于新秀Shiro來(lái)說(shuō),好評(píng)還是比較多的,使用起來(lái)比較簡(jiǎn)單,功能也足夠強(qiáng)大,擴(kuò)展性也較好。聽(tīng)說(shuō)連Spring的官方都不用Spring Security,用的是Shiro,足見(jiàn)Shiro的優(yōu)秀。網(wǎng)上找到兩篇介紹:http://www.infoq.com/cn/articles/apache-shiro http://www.ibm.com/developerworks/cn/opensource/os-cn-shiro/,官網(wǎng)http://shiro.apache.org/ ,使用和配置起來(lái)還是比較簡(jiǎn)單。下面只是簡(jiǎn)單介紹下我們是如何配置和使用Shiro的(暫時(shí)只用到了Shiro的一部分,沒(méi)有配置shiro.ini文件)。

          首先是添加過(guò)濾器,在web.xml中:

          <filter>

          <filter-name>shiroFilter</filter-name>

          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

          <init-param>

                      <param-name>targetFilterLifecycle</param-name>

                      <param-value>true</param-value>

               </init-param>

          </filter>    

          <filter-mapping>

          <filter-name>shiroFilter</filter-name>

          <url-pattern>/*</url-pattern>

          </filter-mapping>

          權(quán)限的認(rèn)證類(lèi):

          public class ShiroDbRealm extends AuthorizingRealm {

              @Inject

              private UserService userService ;

              

              /**

           * 認(rèn)證回調(diào)函數(shù),登錄時(shí)調(diào)用.

           */

          protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) 
          throws AuthenticationException {

          UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

          User useruserService.getUserByUserId(token.getUsername());

          if (user!= null) {  

              return new SimpleAuthenticationInfo(user.getUserId(), user.getUserId(), getName());

          else {

          return null;

          }

          }

          /**

           * 授權(quán)查詢回調(diào)函數(shù), 進(jìn)行鑒權(quán)但緩存中無(wú)用戶的授權(quán)信息時(shí)調(diào)用.

           */

          protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

          String loginName = (String) principals.fromRealm(getName()).iterator().next();

          User useruserService.getUserByUserId(loginName);

          if (user != null) {

          SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

          info.addStringPermission("common-user");

          return info;

          else {

          return null;

          }

          }

          }

          Spring的配置文件:

          <?xml version="1.0" encoding="UTF-8"?>

          <beans >

          <description>Shiro Configuration</description>

          <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"/>

          <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">

          <property name="realm" ref="shiroDbRealm" />

          </bean>

          <bean id="shiroDbRealm" class="com.company.service.common.shiro.ShiroDbRealm" />

              <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">

                  <property name="securityManager" ref="securityManager"/>

                  <property name="loginUrl" value="/common/security/login" />

                  <property name="successUrl" value="/common/security/welcome" />

                  <property name="unauthorizedUrl" value="/common/security/unauthorized"/>

                  <property name="filterChainDefinitions">

                      <value>

                          /resources/** = anon

                          /manageUsers = perms[user:manage]

                          /** = authc

                      </value>

                  </property>

              </bean>

          <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

              <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>

              <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">

                  <property name="securityManager" ref="securityManager"/>

              </bean>

          </beans>

          登錄的Controller:

          @Controller

          @RequestMapping(value = "/common/security/*")

          public class SecurityController {

              @Inject

              private UserService userService;

              @RequestMapping(value = "/login")

              public String login(String loginName, String password,
          HttpServletResponse response, HttpServletRequest request) throws Exception {

                  User user = userService.getUserByLogin(loginName);

                      if (null != user) {

                          setLogin(loginInfoVO.getUserId(), loginInfoVO.getUserId());

                          return "redirect:/common/security/welcome";

                      } else {

                          return "redirect:/common/path?path=showLogin";

                      }

              };

              public static final void setLogin(String userId, String password) {

                  Subject currentUser = SecurityUtils.getSubject();

                  if (!currentUser.isAuthenticated()) {

                      //collect user principals and credentials in a gui specific manner 

                      //such as username/password html form, X509 certificate, OpenID, etc.

                      //We'll use the username/password example here since it is the most common.

                      //(do you know what movie this is from? ;)

                      UsernamePasswordToken token = new UsernamePasswordToken(userId, password);

                      //this is all you have to do to support 'remember me' (no config - built in!):

                      token.setRememberMe(true);

                      currentUser.login(token);

                  }

              };

              

              @RequestMapping(value="/logout")

              @ResponseBody

              public void logout(HttpServletRequest request){

                  Subject subject = SecurityUtils.getSubject();

                  if (subject != null) {           

                      subject.logout();

                  }

                  request.getSession().invalidate();

              };

          }

          注冊(cè)和獲取當(dāng)前登錄用戶:

              public static final void setCurrentUser(User user) {

                  Subject currentUser = SecurityUtils.getSubject();

                  if (null != currentUser) {

                      Session session = currentUser.getSession();

                      if (null != session) {

                          session.setAttribute(Constants.CURRENT_USER, user);

                      }

                  }

              };

              public static final User getCurrentUser() {

                  Subject currentUser = SecurityUtils.getSubject();

                  if (null != currentUser) {

                      Session session = currentUser.getSession();

                      if (null != session) {

                          User user = (User) session.getAttribute(Constants.CURRENT_USER);

                          if(null != user){

                              return user;

                          }

          }

          }

              };

          需要的jar包有3個(gè):shiro-core.jar,shiro-spring.jar,shiro-web.jar。感覺(jué)shiro用起來(lái)比SpringSecurity簡(jiǎn)單很多。

          posted @ 2011-09-16 21:36 GhostZhang 閱讀(32459) | 評(píng)論 (14)編輯 收藏

          在我們開(kāi)發(fā)的一個(gè)系統(tǒng)中,有定時(shí)任務(wù),自然就想到了Quartz,由于框架采用的Spring,Quartz跟Spring的集成也非常簡(jiǎn)單,所以就把Quartz配置到框架中,當(dāng)系統(tǒng)啟動(dòng)后,定時(shí)任務(wù)也就自動(dòng)啟動(dòng)。在開(kāi)發(fā)的過(guò)程中一直沒(méi)有發(fā)現(xiàn)問(wèn)題,但是最后上線的時(shí)候,采用的是weblogic cluster,啟動(dòng)了4個(gè)節(jié)點(diǎn),發(fā)現(xiàn)有的定時(shí)任務(wù)執(zhí)行了不止一次,才恍然大悟,4個(gè)節(jié)點(diǎn)啟動(dòng)了4個(gè)應(yīng)用,也就啟動(dòng)了4個(gè)定時(shí)任務(wù),所以在同一個(gè)時(shí)間定時(shí)任務(wù)執(zhí)行了不止一次。去網(wǎng)上搜索,發(fā)現(xiàn)Quartz也支持cluster,但是我覺(jué)得就我們的系統(tǒng)而言,沒(méi)有必要采用cluster的定時(shí)任務(wù),也許是比較懶吧,就想讓定時(shí)任務(wù)只執(zhí)行一次。在網(wǎng)上搜到了robbin的一篇文章(http://robbin.iteye.com/blog/40989 ),發(fā)現(xiàn)把quartz集中到webapp當(dāng)中還是有一定的風(fēng)險(xiǎn),同時(shí)同一個(gè)時(shí)間點(diǎn)執(zhí)行也不止一次。Robbin的解決辦法就是自己?jiǎn)为?dú)啟動(dòng)一個(gè)Job Server,來(lái)quartz跑job,不要部署在web容器中。 

          我也比較同意這個(gè)辦法。鑒于時(shí)間比較緊,就想有沒(méi)有比較方便的方法。其實(shí)把原來(lái)的webapp當(dāng)做一個(gè)quartz的容器就可以了??梢宰约簩?xiě)一個(gè)線程來(lái)跑應(yīng)用,再寫(xiě)一個(gè)command啟動(dòng)這個(gè)線程就可以了。線程類(lèi)很簡(jiǎn)單,如下:

          public class StartServer {

              public static void main(String[] args) throws Exception {

                  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
          new String[] { "/spring/context-annotation.xml","/spring/context-transaction.xml",
          "/spring/context-hibernate.xml",
          "/spring/context-quartz.xml"});

                  System.out.println("start server....");

                  while (true) {

                      try {

                          Thread.sleep(900);

                      } catch (InterruptedException ex) {

                      }

                  }

              };

          }

          去掉了系統(tǒng)的controller配置servlet.xml,運(yùn)行這個(gè)類(lèi)就可以了。

          在web-inf目錄下寫(xiě)一個(gè)command來(lái)啟動(dòng)這個(gè)java類(lèi):

          setlocal ENABLEDELAYEDEXPANSION

          if defined CLASSPATH (set CLASSPATH=%CLASSPATH%;.) else (set CLASSPATH=.)

          FOR /R .\lib %%G IN (*.jar) DO set CLASSPATH=!CLASSPATH!;%%G

          Echo The Classpath definition is==== %CLASSPATH%

          set CLASSPATH=./classes;%CLASSPATH%

          java com.company.job.StartServer

          這個(gè)command需要把需要的jar(web-inf/lib中)包都放到classpath中。

          每次啟動(dòng)的時(shí)候執(zhí)行這個(gè)command就可以了。跟原來(lái)的應(yīng)用分開(kāi)了,調(diào)試起定時(shí)任務(wù)也不用影響到原來(lái)的應(yīng)用,還是比較方便的。部署的時(shí)候原樣拷貝一份,然后執(zhí)行這個(gè)command就好了,部署起來(lái)也比較方便。

           

          posted @ 2011-09-13 12:53 GhostZhang 閱讀(2669) | 評(píng)論 (3)編輯 收藏

               摘要: Spring MVC經(jīng)過(guò)三個(gè)版本,功能已經(jīng)改進(jìn)和完善了很多。尤其是2.5以來(lái)采用的Annotation的參數(shù)綁定,極大的方便了開(kāi)發(fā),3.0對(duì)其進(jìn)行更進(jìn)一步的完善。對(duì)于一些特殊的前臺(tái)框架,傳到后臺(tái)的不是普通的request中的參數(shù),而是request流中的xml格式,這時(shí)就不能采用SpringMVC自帶的參數(shù)綁定方法。這時(shí)候考慮是否能擴(kuò)展一下。SpringMVC默認(rèn)使用的是Annotati...  閱讀全文
          posted @ 2011-09-12 19:12 GhostZhang 閱讀(4299) | 評(píng)論 (6)編輯 收藏

          主站蜘蛛池模板: 桦甸市| 微山县| 阿拉善盟| 景泰县| 石屏县| 拉萨市| 林州市| 洛浦县| 兰考县| 宁陵县| 新营市| 元江| 浑源县| 出国| 和政县| 泽库县| 紫阳县| 鲜城| 清水县| 文登市| 襄垣县| 拜城县| 广昌县| 吕梁市| 秦安县| 广丰县| 甘肃省| 宁乡县| 丰原市| 中牟县| 峨山| 衡南县| 新和县| 万年县| 北流市| 长治市| 扎鲁特旗| 饶河县| 衡东县| 兴安县| 南宁市|