.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Tag Archives: Java

Configuration ngoài cho ứng dụng với Spring Boot


1. Sử dụng properties file SpringApplication sẽ load các property từ tất cả các file application.properties trong các vị trí sau, và thêm chúng vào application environment: + Folder /config bên trong folder hiện tại. + Folder hiện tại. + Class path /config + Root class path Mặc định, SpringApplication sẽ load theo thứ tự từ dưới lên trên. Và các property nào load sau sẽ override property load trước. Vì lý do này, bạn cứ đặt property vào class path bình thường, khi muốn config ngoài file jar, thì sẽ đặt file application.properties vào folder /config là được. Như vậy cần phải build dự án với maven sao cho có kết quả trong folder target như sau:

+-target
    +---myapp.jar
    +---config
            +--application.properties

Read more of this post

Advertisements

Sử dụng configuration class trong Spring Boot


1. Sử dụng class Configuration
Spring Boot ưu ái việc sử dụng các class Configuration. Mặc dù bạn có thể hoàn toàn sử dụng việc gọi hàm SpringApplication.run() ngay bên trong file XML, nhưng nhìn chung Spring khuyên điểm xuất phát của ứng dụng là một class với annotation @Configuration. Thông thường thì class với hàm main() thì nên là cũng là lớp chứa @Configuration.

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Application
{
    public static void main(final String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

Read more of this post

Extract content and attachments from an Message use Javax mail (with embedded message EML, MSG)


The main part in a javax message is the content. The content can be any Java object representing the mail content, like a plain text (String) or raw image data or even a multiple part object. A multiple part content a message’s content consists of more than a single item. A multiple part object is a container which holds one ore more BodyPart objects. These BodyParts, like a message, have a content and a content type (in fact, both message and BodyPart implement the same interface Part which carries these properties).

Beside plain content, A BodyPart can contain another Multipart or even another Message, a so called nested message (e.g. a forwarded message, called embedded message) with content type ‘message/rfc822’.

As you can see, the structure of a message can be rather heterogeneous, a tree with nodes of different types. The following picture illustrates the tree structure for a sample message.
Read more of this post

Basically usage of properties file in Java project


Properties file is just a file whose extension is properties in a Java project. It simply contains the pair of key-value string separated by a equal character. Properties file is used for obtaining configuration information, interface messages,…for usual. In generally, it is used for some circumstances in which we need to change the information easily. What functions does Java suppose us to do with a properties files? They comprise loading all information stored in properties file and listing them, retrieving any key-value pair, updating the value of a pair. These actions are all via a Properties object. The following snippets depict the functions:
Getting a value from a key:
Read more of this post

How to customize GUI base on user role with spring security


To restrict user from seeing one or more parts on any page you can use a function of spring security that check user login ‘s role. Snippet below is an example of the granted authority checking:

<sec:ifAnyGranted roles="ROLE_ADMIN">
    <li><g:link class="create" action="create">
            <g:message code="default.new.label" args="[entityName]" />
        </g:link></li>
</sec:ifAnyGranted>

The code restrict user from access a button with user role to only ADMIN. If we pre-define the role hierarchy of roles in system, we just only specify one role, otherwise you have to declare all the roles that can see the inside elements.

How to create a tree main menu in grails


Build the GSP page contain the menu with one div tag named mainMenu

<body>
    <div id="spsLogo" role="banner">
        <a href="http://tranhoangminh.vn"><asset:image src="sps_logo.png" alt="Code Snippet" /></a>
    </div>
    <sec:ifLoggedIn>
        <div class="mainMenu">
            <ul>
                <li><a href="${createLink(uri: '/')}" style="text-decoration: none;"> Home</a></li>
                <li><a href="#">Resource </a>
                    <ul>
                        <li><a href="${createLink(action:'index', controller:'customer')}" style="text-decoration: none">Customer</a></li>
                        <li><a href="${createLink(action:'index', controller:'project')}" style="text-decoration: none">Project</a></li>
                        <li><a href="${createLink(action:'index', controller:'document')}" style="text-decoration: none">Document</a></li>
                        <li><a href="${createLink(action:'index', controller:'contact')}" style="text-decoration: none">Contact</a></li>
                    </ul></li>
                <li><a href="${createLink(action:'index', controller:'Search')}">Search</a></li>
                <li><a href="#">Manager</a>
                    <ul>
                        <li><a href="${createLink(action:'viewUser', controller:'User')}" style="text-decoration: none">Add User </a></li>
                        <li><a href="${createLink(action:'viewRole', controller:'User')}" style="text-decoration: none">Manager Role </a></li>
                    </ul></li>
            </ul>
        </div>
    </sec:ifLoggedIn>
    <g:layoutBody />

    <div class="footer" role="contentinfo">
        <small>&copy; Copyright 2014 CRM | Powered by &nbsp;<a>SPS VietNam</a></small>
    </div>
    <div id="spinner" class="spinner" style="display: none;">
        <g:message code="spinner.alt" default="Loading&hellip;" />
    </div>
</body>

Read more of this post

Configure spring security with ldap authentication and local system roles in grails


Add plugins Add plugins of Spring security and ldap in BuildConfig

plugins {
        compile ":spring-security-core:2.0-RC4"
        compile ":spring-security-ldap:2.0-RC2"
    }

Configure spring security
Configure spring security and LDAP authentication in Config
Read more of this post

%d bloggers like this: